zoukankan      html  css  js  c++  java
  • 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; }
    
        }
    

      

     

  • 相关阅读:
    allegro把formate symbol文件从一个文件拷入另一个文件的方法
    allegro17.2 gerber 步骤
    MIPI-Layout说明(转载)
    libdivsufsort 简介
    Perforce 的基本使用教程
    Linux 信息查询
    PD(Power Delivery)充电协议
    【转】升级还是权谋?从USB PD 2.0到3.0
    《很杂很杂的杂学知识》--张立新
    《梅赛德斯先生》 --史蒂芬逊.金
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/11910695.html
Copyright © 2011-2022 走看看