开发者

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

开发者 https://www.devze.com 2023-02-09 09:41 出处:网络
Is there any way to generate a ToString() using Visual Studio 2010? I really don\'t want to do this by hand!

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 simple string representation of my model. In previous IDEs * ToString generation has been enabled in the UI using simple templates and field selection.

Currently default implementations of Equals and Hashcode are offer开发者_如何学编程ed in a similar pattern. I'd hoped there was something similar for ToString.

It seems not by default - thanks for the responses!

(* this is my first .net project)


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.


With Reflection you can actually code a ToString() method:

public override String ToString()
{
    Type objType = this.GetType();
    PropertyInfo[] propertyInfoList = objType.GetProperties();
    StringBuilder result = new StringBuilder();
    foreach (PropertyInfo propertyInfo in propertyInfoList)
         result.AppendFormat("{0}={1} ", propertyInfo.Name, propertyInfo.GetValue(this));

     return result.ToString();
}


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);
  }
}


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.


Major pain that VS 2010 doesn't even have an autogenerate ToString method, syntax is close enough to Java where I used Ecilpse to Generate the ToString and then pasted it into VS...


It doesn't exist on VS out of the box, but it does on the ReSharper plugin, if you don't want to implement it yourself. The plugin is commercial, but I personally think it is worth the money.

With ReSharper, it would be alt+ins -> overriding members -> tostring while class name is on the cursor.


Maybe you should take a look at AutoCode 4.0. It is a Visual Studio extension which will brings some snippets with it.

For example you could somewhere within your class simply write tostr and press Ctrl+Enter and it will automatically generate the ToString() method that concatenates all public properties of the class.


If you need better representation of your object while debugging you can use the DebuggerDisplayAttribute:

[DebuggerDisplay("Count = {count}")]
class MyHashtable
{
    public int count = 4;
}

This can be quicker than overriding ToString, but it still doesn't let you choose fields, you have to type them.


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.


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消