【发布时间】:2010-05-09 11:04:18
【问题描述】:
[MyAttribute()]
public string Name { get; set; }
在MyAttribute我需要知道关联属性的名称,可以吗?
编辑:
我需要在文本格式中使用它。
【问题讨论】:
-
你能详细说明你为什么需要它以及你会用它做什么吗?
标签: .net attributes
[MyAttribute()]
public string Name { get; set; }
在MyAttribute我需要知道关联属性的名称,可以吗?
编辑:
我需要在文本格式中使用它。
【问题讨论】:
标签: .net attributes
不,这是不可能的。通常您会使用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
}
}
【讨论】: