枚举GetCustomAttributes不显示任何内容
c#
在这个简单的类中,我尝试覆盖 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自定义属性吗?如果类变大,是否有任何简单的枚举?
回答
属性是这样的:
[MyCoolAttribute]
public MyCoolMethod()
您正在寻找的是属性
this.GetType().GetProperties()