【发布时间】:2021-10-20 22:48:54
【问题描述】:
在这个简单的类中,我尝试覆盖 ToString() 以显示所有自定义属性。
public class TryMe
{
public TryMe(int id, List<String> tests)
{
ID = id;
Tests = tests;
}
public int ID { get; set; }
public List<String> Tests { get; set; }
public override string ToString()
{
string me = "";
var attributes = this.GetType().GetCustomAttributes();
foreach (var attribute in attributes)
{
me = me + attribute.ToString() + ",";
}
return me;
}
}
它没有显示任何值或错误。
ID 和 Tests 不是自定义属性吗?
如果类变得更大,是否有任何简单的枚举?
【问题讨论】:
-
在这种情况下你也应该使用 StringBuilder,效率更高!
-
@jason.kaisersmith 在这种情况下,
StringBuilder几乎肯定会更慢。当您进行 6 或 7 个字符串操作时,它往往会更好,因此您需要更多属性。当然,当性能受到关注时,YMMV 和测试总是有用的。
标签: c#