Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何读取DisplayAttribute.Name属性?_C# - Fatal编程技术网

C# 如何读取DisplayAttribute.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)

这似乎是一个相当琐碎的任务,但是我看不到如何在代码中读取displaytribute.Name属性

我有一个具有属性的类:

[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());