.NET:在属性中获取属性名称

Fer*_*ryt 6 .net attributes

[MyAttribute()]
public string Name { get; set; }
Run Code Online (Sandbox Code Playgroud)

MyAttribute我需要知道相关属性的名称,这可能吗?

编辑:

我需要在文本格式中使用它.

Dar*_*rov 8

不,这是不可能的.通常,您将使用反射来读取应用于给定属性的属性,因此您已经知道该属性.例:

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)