lau*_*ura 36 c# visual-studio-2010
有没有办法生成ToString()使用Visual Studio 2010?
我真的不想手工做这个!
[编辑]
我正在寻找我的模型的简单字符串表示.在以前的IDE中,使用简单模板和字段选择在UI中启用了ToString生成.
目前,Equals和Hashcode的默认实现以类似的模式提供.我希望ToString有类似的东西.
它似乎不是默认 - 感谢您的回复!
(*这是我的第一个.net项目)
tst*_*ter 23
Resharper通过生成"格式化成员"来支持此功能
https://www.jetbrains.com/resharper/webhelp/Code_Generation__Formatting_Members.html
Resharper -> Edit -> Generate Code -> Formatting Members
Run Code Online (Sandbox Code Playgroud)
要么
alt + insert -> Formatting Members
Run Code Online (Sandbox Code Playgroud)
我确认这可以在Resharper 8中找到.
您可以使用StatePrinter项目
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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
使用反射,您实际上可以编写ToString()方法:
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();
}
Run Code Online (Sandbox Code Playgroud)
您可以为每个样板代码创建自己的自定义代码段并从IntelliSence访问它.
这是一个很好的教程http://msdn.microsoft.com/en-us/library/ms165392.aspx
详细了解如何使用替换创建片段.您可以创建非常通用的结构.
| 归档时间: |
|
| 查看次数: |
24523 次 |
| 最近记录: |