C# 如何读取DisplayAttribute.Name属性?
这似乎是一个相当琐碎的任务,但是我看不到如何在代码中读取displaytribute.Name属性 我有一个具有属性的类:C# 如何读取DisplayAttribute.Name属性?,c#,C#,这似乎是一个相当琐碎的任务,但是我看不到如何在代码中读取displaytribute.Name属性 我有一个具有属性的类: [Display(Name="A Nice Field")] public string SomeField{get;set;} 在我的代码中,我遇到了一个路障 object[] attrs = prop.GetCustomAttributes(true); foreach (Attribute attr in attrs)
[Display(Name="A Nice Field")]
public string SomeField{get;set;}
在我的代码中,我遇到了一个路障
object[] attrs = prop.GetCustomAttributes(true);
foreach (Attribute attr in attrs)
{
if (attr is DisplayAttribute)
{
// How do I read the attr Name property?
}
}
string name=((DisplayAttribute)attr.name
?
var displayAttribute = (DisplayAttribute)(prop.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault());