Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I generate a ToString() in Visual Studio that includes all properties?

I'm a Java Developer, used to the 'generate toString()' option in Eclipse which offers a complete toString, printing values of all instance variables. I'm just looking for the comparable shortcut in Visual Studio.

I've seen how you can begin typing the method, "public override " and autocomplete will stub a ToString() but it will not fill it in with all the class properties.

    public override string ToString()
        {
            return base.ToString();
        }

I'd like the generated method to include all properties of the class.

like image 401
HoosierDude Avatar asked Oct 30 '19 10:10

HoosierDude


1 Answers

You could use JSON.NET to serialize your class.

public override string ToString()
{
    return JsonConvert.SerializeObject(this);
}
like image 156
Rawitas Krungkaew Avatar answered Nov 15 '22 10:11

Rawitas Krungkaew