Is there a ToString() generator available in Visual Studio 2010?

前端 未结 11 747
伪装坚强ぢ
伪装坚强ぢ 2021-01-03 17:39

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

相关标签:
11条回答
  • 2021-01-03 18:12

    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);
      }
    }
    
    0 讨论(0)
  • 2021-01-03 18:13

    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.

    0 讨论(0)
  • 2021-01-03 18:14

    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.

    0 讨论(0)
  • 2021-01-03 18:15

    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.

    0 讨论(0)
  • 2021-01-03 18:19

    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.

    0 讨论(0)
  • 2021-01-03 18:21

    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.

    0 讨论(0)
提交回复
热议问题