使用EntityFramework,如何访问DisplayName的外键列? [英] With EntityFramework, how to access DisplayName for foreign key columns?

查看:224
本文介绍了使用EntityFramework,如何访问DisplayName的外键列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下注释:

    [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

我使用以下方法获取显示名称:

I use the following method to get the display name :

    PropertyInfo pi = SomeObject.GetProperties[0]; // short example
    columnName = ReflectionExtensions.GetDisplayName(pi);

它适用于之外的所有列,代码没有找到自定义/显示属性对于诸如PhoneModel1之类的列,即使有一个属性清楚。它适用于 int?,但是我不需要 id 的标题,我需要在PhoneModel1中实际值的标题。

It works for all columns except the code finds no custom/display attribute for columns such as PhoneModel1 even if there is clearly one attribute. It works for the int? but I don't need the header for the id, I need the header for the actual value, which is in 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;
                }
            }
        }
    }


推荐答案

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

Your GetDisplayName extension method should look like this:

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;
}

并使用它:

PropertyInfo pi = SomeObject.GetProperties[0];
string columnName = pi.GetDisplayName();

请注意,如果该属性未定义 DisplayName 属性我们返回属性名。

Note that if the property doesn't define a DisplayName attribute we return the property name.

这篇关于使用EntityFramework,如何访问DisplayName的外键列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆