当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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 offered 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)

网友答案:

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.

网友答案:

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.

网友答案:

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.

网友答案:

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);
  }
}
网友答案:

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...

网友答案:

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.

网友答案:

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

public string ToString(MyObjectClass object)
{
    Type MyObjectType = object.GetType();
    PropertyInfo[] propertyInfoList = MyObjectType.GetProperties();
    string result = "";
    foreach(PropertyInfo propertyInfo in propertyInfoList)
    {
        result += string.Format("{0}={1} ", propertyInfo.Name, propertyInfo.GetValue(sheetDbEntry));
    }

    return result;
}
网友答案:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: