[MyAttribute()]
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)
在MyAttribute我需要知道相关属性的名称,这可能吗?
编辑:
我需要在文本格式中使用它.
不,这是不可能的.通常,您将使用反射来读取应用于给定属性的属性,因此您已经知道该属性.例:
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
}
}
Run Code Online (Sandbox Code Playgroud)