3

I have two objects I need to assert using NUnit. Object class has properties that I populate continuously during development. I use Microsoft Visual Studio Community 2019 and am able to generate HashCode and Equals automatically. I used to work with Java and was able to generate toString by IDE automatically, switched recently to C#.

The question

Is there a way to generate ToString() automatically in a similar way as HashCode and Equals?

Currently I populate ToString() manually once adding a new property, but the more properties I add the harder it becomes to track all fields in ToString method.

I was searching for a solution and was able to find outdated posts only.

Lia
  • 504
  • 2
  • 7
  • Do you _need_ to populate these methods? – ProgrammingLlama Apr 06 '21 at 00:47
  • @Llama Yes I need populate all these three methods – Lia Apr 06 '21 at 00:50
  • 1
    Does this answer your question? [Is there a ToString() generator available in Visual Studio 2010?](https://stackoverflow.com/questions/4932136/is-there-a-tostring-generator-available-in-visual-studio-2010) – FriedLychees Apr 06 '21 at 00:55
  • 1
    you can do https://stackoverflow.com/questions/9299286/dynamic-override-of-tostring-using-reflection which will use reflection ( you can customize the formatting ). If you do it as an extension method like "AsString" you can have it on all your classes in one shot – Keith Nicholas Apr 06 '21 at 01:01
  • 1
    Generally you don't. Maybe display the name and/or the Id ... but *all* the fields?? See also `[DebuggerDisplay]` attribute for a way to display information while debugging without impacting `ToString()`. If this is for equality comparison, see https://stackoverflow.com/questions/318210/compare-equality-between-two-objects-in-nunit – Ian Mercer Apr 06 '21 at 01:21
  • 1
    @KeithNicholas thank you for your response, taking a look at that – Lia Apr 06 '21 at 01:27
  • @IanMercer thank you for your response, I don't have an issue with asserting two objects, functionality with assertion works fine, but the issue is if objects are not the same and need to see what exact property is not as expected without debugging, especially when tests are running on CI. – Lia Apr 06 '21 at 01:33
  • Take a look at `FluentAssertions` which provides some very nice extra functionality for testing object graphs: https://fluentassertions.com/objectgraphs/ – Ian Mercer Apr 06 '21 at 01:47
  • @FriedLychees thank you for your response, can I refer for answer for VisualStudio 2010 and question that was asked 10 years ago? VisualStudio 2010 and VisualStudio 2019 have no difference? – Lia Apr 06 '21 at 01:53
  • 1
    Please note that one of the reasons it's off-topic to ask for recommendations for things like tools, libraries, etc. is that, besides producing a variety of opinion-based answers, the answers cannot be kept up to date. Some features disappear, others appear in the future, and either way the question does not remain useful for future readers. Your question is one of those kinds of questions, so even if it were not already a duplicate, it would be closed for the other reason. See meta discussion like https://meta.stackoverflow.com/q/252520 and https://meta.stackoverflow.com/q/255198 – Peter Duniho Apr 06 '21 at 01:58
  • And https://meta.stackoverflow.com/questions/260718/duplicates-of-old-cobwebbed-questions, https://meta.stackoverflow.com/questions/361692/when-a-question-is-teeming-with-out-of-date-answers, and https://meta.stackoverflow.com/questions/263122/what-should-i-do-if-my-popular-question-is-out-of-date – Peter Duniho Apr 06 '21 at 01:59
  • As far as your exact question goes, Microsoft isn't hiding features from you. If there were a generator such as you're looking for, you'd be able to find it in the product documentation and/or the user interface. Questions asking the Stack Overflow community to do your own research for you aren't suitable for the site, even ignoring the other issues mentioned above. – Peter Duniho Apr 06 '21 at 02:01

0 Answers0