Asp.net mvc 使用EntityFramework,如何访问外键列的DisplayName?

Asp.net mvc 使用EntityFramework,如何访问外键列的DisplayName?,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我有以下注解: [Display(Name = "NotImportant", ResourceType = typeof(MyResxFile))] public int? PhoneModel { get; set; } // this is the id [Display(Name = "Important", ResourceType = typeof(MyResxFile))] public virtual PhoneModel PhoneModel1

我有以下注解:

    [Display(Name = "NotImportant", ResourceType = typeof(MyResxFile))]
    public int? PhoneModel { get; set; } // this is the id
    [Display(Name = "Important", ResourceType = typeof(MyResxFile))]
    public virtual PhoneModel PhoneModel1 { get; set; } // this is the object
我使用以下方法获取显示名称:

    PropertyInfo pi = SomeObject.GetProperties[0]; // short example
    columnName = ReflectionExtensions.GetDisplayName(pi);
它适用于除之外的所有列。代码找不到诸如PhoneModel1之类的列的自定义/显示属性,即使有一个属性。它适用于int?,但我不需要id的标题,我需要实际值的标题,它在PhoneModel1中

    public static class ReflectionExtensions
    {

        public static T GetAttribute<T>(this MemberInfo member, bool isRequired)
            where T : Attribute
        {
            var attribute = member.GetCustomAttributes(typeof(T), false).SingleOrDefault();

            if (attribute == null && isRequired)
            {
                throw new ArgumentException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "The {0} attribute must be defined on member {1}",
                        typeof(T).Name,
                        member.Name));
            }

            return (T)attribute;
        }

        public static string GetDisplayName(PropertyInfo memberInfo)
        {
            var displayAttribute = memberInfo.GetAttribute<DisplayAttribute>(false);

            if (displayAttribute != null)
            {
                ResourceManager resourceManager = new ResourceManager(displayAttribute.ResourceType);
                var entry = resourceManager.GetResourceSet(Thread.CurrentThread.CurrentUICulture, true, true)
                                           .OfType<DictionaryEntry>()
                                           .FirstOrDefault(p => p.Key.ToString() == displayAttribute.Name);

                return entry.Value.ToString();
            }
            else
            {
                var displayNameAttribute = memberInfo.GetAttribute<DisplayNameAttribute>(false);
                if (displayNameAttribute != null)
                {
                    return displayNameAttribute.DisplayName;
                }
                else
                {
                    return memberInfo.Name;
                }
            }
        }
    }
公共静态类ReflectionExtensions
{
public static T GetAttribute(需要此MemberInfo成员bool)
其中T:Attribute
{
var attribute=member.GetCustomAttributes(typeof(T),false).SingleOrDefault();
if(属性==null&&isRequired)
{
抛出新的ArgumentException(
字符串格式(
CultureInfo.InvariantCulture,
“必须在成员{1}上定义{0}属性”,
类型(T)。名称,
成员(姓名);
}
返回(T)属性;
}
公共静态字符串GetDisplayName(PropertyInfo memberInfo)
{
var displayAttribute=memberInfo.GetAttribute(false);
if(displayAttribute!=null)
{
ResourceManager ResourceManager=新的ResourceManager(displayAttribute.ResourceType);
var entry=resourceManager.GetResourceSet(Thread.CurrentThread.CurrentUICulture,true,true)
第()类
.FirstOrDefault(p=>p.Key.ToString()==displayAttribute.Name);
返回entry.Value.ToString();
}
其他的
{
var displayNameAttribute=memberInfo.GetAttribute(false);
if(displayNameAttribute!=null)
{
返回displayNameAttribute.DisplayName;
}
其他的
{
返回memberInfo.Name;
}
}
}
}
试试这个:

var attribute =typeof(MyClass).GetProperty("Name").GetCustomAttributes(typeof(DisplayNameAttribute), true).Cast<DisplayNameAttribute>().Single();
string displayName = attribute.DisplayName; 
var attribute=typeof(MyClass).GetProperty(“Name”).GetCustomAttributes(typeof(DisplayNameAttribute),true.Cast().Single();
字符串displayName=attribute.displayName;

希望对您有所帮助。

您的
GetDisplayName
扩展方法应该如下所示:

public static string GetDisplayName(this PropertyInfo pi)
{
    if (pi == null)
    {
        throw new ArgumentNullException(nameof(pi));
    }
    return pi.IsDefined(typeof(DisplayAttribute)) ? pi.GetCustomAttribute<DisplayAttribute>().GetName() : pi.Name;
}

请注意,如果属性没有定义
DisplayName
属性,我们将返回属性名称。

非常感谢,您让我开心!神奇之处在于使用GetName()而不是.Name
PropertyInfo pi = SomeObject.GetProperties[0];
string columnName = pi.GetDisplayName();