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

      

     

  • 相关阅读:
    VUE 腾讯云 web端上传视频SDK 上传进度无法显示
    SQL Server 连接数据库报错 (ObjectExplorer)
    Docker 下,搭建 SonarQube 环境 (数据库为 postgres)
    静态代码扫描工具
    windows10 中为文件添加让自己可以使用查看、修改、运行的权限
    mysql8.0.21下载安装详细教程,mysql安装教程
    C# 关于构造函数引证次序讨教
    c# winfrom 读取app.config 自定义节点
    如何在C#中将项目添加到列表中
    如何在C#实现日志功能
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/11910695.html
Copyright © 2011-2022 走看看