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

      

     

  • 相关阅读:
    ORA12514: TNS: 监听程序当前无法识别连接描述符中请求的服务
    ORACLE MERGE 介绍
    【转】三大UML建模工具Visio、Rational Rose、PowerDesign的区别
    ORALC的STDDEV、STDDEV_POP、STDDEV_SAMP等函数
    SQL语言艺术实践篇——局外思考
    数据分析方法
    ORACLE FLASHBACK TABLE 的一个有趣问题
    avalon有关ViewModel与Model的同步问题
    迷你MVVM框架 avalonjs 0.73发布
    将一个节点集合以最少的步骤转换为另一个节点集合
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/11910695.html
Copyright © 2011-2022 走看看