[MyAttribute()]
public string Name { get; set; }
在
MyAttribute
我需要知道相关属性(property)的名称,这可能吗?编辑:
我需要在文本格式中使用它。
最佳答案
不,这是不可能的。通常你会使用 reflection to read attributes应用于给定的属性,因此您已经知道该属性。例子:
var properties = typeof(SomeType).GetProperties();
foreach (var property in properties)
{
var attributes = property.GetCustomAttributes(typeof(MyAttribute), true);
if (attributes.Count > 0)
{
// look at property.Name here
}
}
关于.NET : Get property name in attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2797429/