Is there any way to generate a ToString()
using Visual Studio 2010?
I really don\'t want to do this by hand!
[EDIT]
I\'m looking for a s
You can use the StatePrinter project
class AClassWithToString
{
string B = "hello";
int[] C = {5,4,3,2,1};
// Nice stuff ahead!
static readonly StatePrinter printer = new StatePrinter();
public override string ToString()
{
return printer.PrintObject(this);
}
}
Resharper supports this by generating "formatting members"
https://www.jetbrains.com/resharper/webhelp/Code_Generation__Formatting_Members.html
Resharper -> Edit -> Generate Code -> Formatting Members
or
alt + insert -> Formatting Members
I confirmed this is available in Resharper 8.
If you don't write your own ToString method, Object
provides one for you (although not very useful, since it only return the namespace and name of the objects type).
Otherwise, you have to create it yourself, since the IDE cannot possibly know what you want to output as an object's ToString
method.
You can create your own custom snippet for every boilerplate code and access it from IntelliSence
Here is a good tutorial http://msdn.microsoft.com/en-us/library/ms165392.aspx
Have a good look at how to create snippets with replacements. You can create quite generic structures.
Since the logic of the overridden ToString()
method would depend on your own business needs, the only thing I could imagine is an add-in which creates the empty ToString()
override for you calling base.ToString()
inside, if you then do not customize its content it does not make any sense to have it like that.
Visual Studio already helps you a lot, at least in C#, if you start typing public overrides.
ToString()
is a method sitting on object
so you do not need to add that to all your classes, only if you need to override and change behaviour.