C# 获取DisplayName属性的所有值

C# 获取DisplayName属性的所有值,c#,asp.net-mvc,entity-framework-6,data-annotations,C#,Asp.net Mvc,Entity Framework 6,Data Annotations,我正在使用asp.net MVC 5和EntityFramework 6 DataAnnotations。 我想知道是否有办法获取对象的所有DisplayName,并将它们保存在变量中到控制器类中 例如,考虑到班级: public class Class1 { [DisplayName("The ID number")] public int Id { get; set; } [DisplayName("My Value")] public int Value

我正在使用asp.net MVC 5和EntityFramework 6 DataAnnotations。 我想知道是否有办法获取对象的所有
DisplayName
,并将它们保存在变量中到控制器类中

例如,考虑到班级:

public class Class1
{
    [DisplayName("The ID number")]
    public int Id { get; set; }

    [DisplayName("My Value")]
    public int Value { get; set; }

    [DisplayName("Label name to display")]
    public string Label { get; set; }
}
如何获取所有属性的
DisplayName
值?例如,如何创建一个函数,该函数返回一个
字典
,该字典的键具有属性名称,值具有
DisplayName
,如下所示:

{ "Id": "The ID name", "Value": "My Value", "Label": "Label name to display"}.

我已经看过这个主题,但我不知道如何扩展此代码。

如果您不真正关心
DisplayName
属性,而是将使用的有效显示名称(例如通过数据绑定),最简单的方法是使用:

var info=TypeDescriptor.GetProperties(typeof(Class1))
.Cast()
.ToDictionary(p=>p.Name,p=>p.DisplayName);

如果您并不真正关心
DisplayName
属性,而是要使用有效的显示名称(例如通过数据绑定),最简单的方法是使用:

var info=TypeDescriptor.GetProperties(typeof(Class1))
.Cast()
.ToDictionary(p=>p.Name,p=>p.DisplayName);

您可以使用下面的代码-

 Class1 c = new Class1();
 PropertyInfo[] listPI = c.GetType().GetProperties();
 Dictionary<string, string> dictDisplayNames = new Dictionary<string, string>();
 string displayName = string.Empty;

 foreach (PropertyInfo pi in listPI)
 {
    DisplayNameAttribute dp = pi.GetCustomAttributes(typeof(DisplayNameAttribute), true).Cast<DisplayNameAttribute>().SingleOrDefault();
            if (dp != null)
            {
                displayName = dp.DisplayName;
                dictDisplayNames.Add(pi.Name, displayName);
            }
 }
class1c=newclass1();
PropertyInfo[]listPI=c.GetType().GetProperties();
Dictionary dictDisplayNames=新字典();
string displayName=string.Empty;
foreach(listPI中的PropertyInfo pi)
{
DisplayNameAttribute dp=pi.GetCustomAttributes(typeof(DisplayNameAttribute),true).Cast().SingleOrDefault();
如果(dp!=null)
{
displayName=dp.displayName;
dictDisplayNames.Add(pi.Name,displayName);
}
}
我也提到了你在问题中提到的同一个链接

最后一本词典如下:


您可以使用下面的代码-

 Class1 c = new Class1();
 PropertyInfo[] listPI = c.GetType().GetProperties();
 Dictionary<string, string> dictDisplayNames = new Dictionary<string, string>();
 string displayName = string.Empty;

 foreach (PropertyInfo pi in listPI)
 {
    DisplayNameAttribute dp = pi.GetCustomAttributes(typeof(DisplayNameAttribute), true).Cast<DisplayNameAttribute>().SingleOrDefault();
            if (dp != null)
            {
                displayName = dp.DisplayName;
                dictDisplayNames.Add(pi.Name, displayName);
            }
 }
class1c=newclass1();
PropertyInfo[]listPI=c.GetType().GetProperties();
Dictionary dictDisplayNames=新字典();
string displayName=string.Empty;
foreach(listPI中的PropertyInfo pi)
{
DisplayNameAttribute dp=pi.GetCustomAttributes(typeof(DisplayNameAttribute),true).Cast().SingleOrDefault();
如果(dp!=null)
{
displayName=dp.displayName;
dictDisplayNames.Add(pi.Name,displayName);
}
}
我也提到了你在问题中提到的同一个链接

最后一本词典如下:



DisplayName
Display
(它们不同)?@IvanStoev
DisplayName
Display
(它们不同)?@IvanStoev
DisplayName
感谢您的回答。我已经封装了用于处理泛型类的代码,并且它继续工作。你解决了我的问题!是否有可能获得一个包含显示名值和函数本身引用的字典?即使这仅适用于静态方法。@Reiner不确定我是否遵循,您能给出一个例子吗?让我们假设
[DisplayName(“ID number”)]public static int ID{get;set;}
方法将作为条目对象添加到字典
字典中:
{“ID number”,Class1.ID}
并且可以与
int id=dic[“id number”]
@Reiner一起使用,因此您需要一个显示名称为键、值为。。。嗯,不知道是什么。很可能你需要的是可能的,但是考虑一下你自己的问题,具体的要求。谢谢你的回答。我已经封装了用于处理泛型类的代码,并且它继续工作。你解决了我的问题!是否有可能获得一个包含显示名值和函数本身引用的字典?即使这仅适用于静态方法。@Reiner不确定我是否遵循,您能给出一个例子吗?让我们假设
[DisplayName(“ID number”)]public static int ID{get;set;}
方法将作为条目对象添加到字典
字典中:
{“ID number”,Class1.ID}
并且可以与
int id=dic[“id number”]
@Reiner一起使用,因此您需要一个显示名称为键、值为。。。嗯,不知道是什么。最有可能的是你可能需要的,但是考虑你自己的问题,具体的要求。谢谢你的答案,但不幸的是你的代码在我的项目上不起作用。问题是什么?它返回“序列中包含没有元素”的DelpRead分配到FACHACH循环。哦!抱歉@Cyr有一个输入错误哦!抱歉@Cyr有一个打字错误,我已经在答案中编辑了。它位于foreach循环内-displayName=pi.GetCustomAttributes(typeof(DisplayNameAttribute),true).Cast().Single().displayName;谢谢你的回答,但不幸的是,你的代码在我的项目中不起作用。有什么问题吗?它将displayName赋值中的“Sequence contains no elements”返回到foreach循环中。哦!抱歉@Cyr有一个输入错误哦!抱歉@Cyr有一个打字错误,我已经在答案中编辑了。它位于foreach循环内-displayName=pi.GetCustomAttributes(typeof(DisplayNameAttribute),true).Cast().Single().displayName;