其他分享
首页 > 其他分享> > DisplayAttribute应用——根据PropertyName自动获取对应的UI显示名

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

作者:互联网

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

    }

  

 

 

 

 

 

 

 

标签:Name,PropertyName,DisplayAttribute,UI,result,PropertyDetail,var,public
来源: https://www.cnblogs.com/panpanwelcome/p/11910695.html