码迷,mamicode.com
首页 > 其他好文 > 详细

DisplayAttribute应用——根据PropertyName自动获取对应的UI显示名

时间:2019-11-22 13:22:32      阅读:56      评论:0      收藏:0      [点我收藏+]

标签:get   prope   request   pre   invoice   根据   res   null   mem   

model定义,使用DisplayAttribute

    public class AddressSetInfo
    {
        /// <summary>
        /// invoiceAddress.Id
        /// </summary>
        [Display(Name = "Id")]
        public virtual Int64 Id { get; set; }
    }    

 

enum定义,使用DisplayAttribute

    public enum CustomerFinancialStatementCurrencyType
    {
        /// <summary>
        /// 人民币,CNY
        /// </summary>
        [Display(Name = "CNY")]
        RMB = 1,
        /// <summary>
        /// 美元
        /// </summary>
        [Display(Name = "USD")]
        USD = 2
    }

  

 

解析DisplayAttribute for class

        /// <summary>
        /// GetPropertyNameDic
        /// </summary>
        /// <typeparam name="T">dictionary. key:PropertyName; value:PropertyDetail
        /// </typeparam>
        /// <returns></returns>
        public static Dictionary<string, PropertyDetail> GetPropertyNameDic<T>()
        {
            var result = new Dictionary<string, PropertyDetail>();
            try
            {
                var typeOfObject = typeof(T);
                var pds = typeOfObject.GetProperties();
                if (pds == null)
                {
                    return result;
                }
                foreach (var p in pds)
                {
                    var propertyItem = new PropertyDetail() {
                        BelongToClassFullName=typeOfObject.FullName,
                        PropertyName=p.Name
                    };
                    var attr = p.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute;
                    if (attr != null)
                    {
                        propertyItem.DisplayName = attr.Name;
                    }
                    result.Add(p.Name, propertyItem);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

 

解析DisplayAttribute for enum

        /// <summary>
        /// GetEnumNameDicForDisplayAttr
        /// </summary>
        /// <param name="enumClassType">dictionary. key:enum Value; value:PropertyDetail</param>
        /// <returns></returns>
        public static Dictionary<string, PropertyDetail> GetEnumNameDicForDisplayAttr(Type enumClassType)
        {
            if (enumClassType == null)
            {
                throw new Exception("request is null");
            }
            if(!enumClassType.IsEnum)
            {
                throw new Exception("request not enum type");
            }
            var result = new Dictionary<string, PropertyDetail>();
            try
            {
                var enumValues = enumClassType.GetEnumValues();
                foreach (var value in enumValues)
                {
                    MemberInfo memberInfo =
                        enumClassType.GetMember(value.ToString()).First();

                    var valueItem = new PropertyDetail()
                    {
                        BelongToClassFullName = enumClassType.FullName,
                        PropertyName = memberInfo.Name
                    };
                    var descriptionAttribute =
                        memberInfo.GetCustomAttribute<DisplayAttribute>();
                    if (descriptionAttribute != null)
                    {
                        valueItem.DisplayName = descriptionAttribute.Name;
                    }
                    result.Add(value.ToString(), valueItem);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

    }

  

辅助model定义

    /// <summary>
    /// PropertyDetail
    /// </summary>
    public class PropertyDetail
    {
        /// <summary>
        /// BelongToClassName
        /// </summary>
        public string BelongToClassFullName { get; set; }

        /// <summary>
        /// PropertyName
        /// </summary>
        public string PropertyName { get; set; }

        /// <summary>
        /// DisplayName
        /// </summary>
        public string DisplayName { get; set; }

    }

  

 

 

 

 

 

 

 

DisplayAttribute应用——根据PropertyName自动获取对应的UI显示名

标签:get   prope   request   pre   invoice   根据   res   null   mem   

原文地址:https://www.cnblogs.com/panpanwelcome/p/11910695.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!