public class UserClass
    {
       
        [DisplayName("名称")] //DisplayName
        public string Name { get; set; }
        [Display(Name = "年龄")]//Display
        public string Age { get; set; }
        [Display(Name = "性别")]
        public string Sex { get; set; }
        [Display(Name = "地址")]
        public string Address { get; set; }
        [Display(Name = "手机号")]
        public string Phone { get; set; }
        [Display(Name = "邮箱")]
        public string Email { get; set; }
    }

代码

/// <summary>
        /// 动态获取 DisplayName和Display(Name='')
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static List<Dictionary<string, string>> GetClassDesc<T>(T t)
        {
            List<Dictionary<string, string>> dicList = new List<Dictionary<string, string>>();
            Dictionary<string, string> dic;
            Type type = t.GetType();
            PropertyInfo[] ProInfo = type.GetProperties();
            foreach (var item in ProInfo)
            {
                if (dicList.Count > 0)
                {
                    //获取Display(Name='')
                    dic = new Dictionary<string,string>();
                    var attribute = type.GetProperty(item.Name);
                    var displayName = attribute.GetCustomAttribute<DisplayAttribute>();
                    dic.Add(item.Name, displayName.Name);
                    dicList.Add(dic);
                }
                else
                {
                    //获取 DisplayName
                    dic = new Dictionary<string, string>();
                    var attribute = type.GetProperty(item.Name);
                    var displayName = attribute.GetCustomAttribute<DisplayNameAttribute>();
                    dic.Add(item.Name, displayName.DisplayName);
                    dicList.Add(dic);
                }
            }
            return dicList;
        }

//调用代码

List<Dictionary<string, string>> dicList = GetClassDesc<UserClass>(Us);
            
            foreach (Dictionary<string,string> item in dicList)
            {
                string Name = item["Name"];//名称
            }

读取 DisplayName和Display(Name='')的更多相关文章

  1. C# 6 与 .NET Core 1.0 高级编程 - 41 ASP.NET MVC(中)

    译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 41 ASP.NET MVC(中)),不对的地方欢迎指出与交流. 章节出自<Professional C# ...

  2. asp.net core 2.1认证

    asp.net core 2.1认证 这篇文章基于asp.net core的CookieAuthenticationHandler来讲述. 认证和授权很相似,他们的英文也很相似,一个是Authenti ...

  3. SpringBoot核心注解应用

    1.今日大纲 了解Spring的发展 掌握Spring的java配置方式 学习Spring Boot 使用Spring Boot来改造购物车系统 2.Spring的发展 Spring1.x 时代 在S ...

  4. Spring Boot属性文件配置文档(全部)

    This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...

  5. 了解SpringBoot

    一.SpringBoot是什么? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...

  6. APS.NET MVC + EF (08)---数据注解和验证

    对于Web开发人员来说,用户输入验证一直是一个挑战.不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.如果觉得验证是令人望而生畏的繁杂琐事,ASP.NET MVC框架提供了数据注解的方式帮助 ...

  7. springboot的简单了解与使用

    1. Spring Boot 1.1. 什么是Spring Boot 1.2. Spring Boot的优缺点 1.3. 快速入门 1.3.1. 设置spring boot的parent <pa ...

  8. OPC UA 统一架构) (二)

    OPC UA (二) 重头戏,捞取数据,才是该干的事.想获取数据,先有数据源DataPrivade,DataPrivade的数据集合不能和BaseDataVariableState的集合存储同一地址, ...

  9. SEO

    白帽SEO 内容优化 网站标题.关键字.描述 网站内容优化 Robot.txt文件 网站地图 增加外链引用 2. 网站结构布局优化 网站加载速度:一个页面<100k 扁平化:网站 目录层次 少, ...

随机推荐

  1. scanf和cin性能的比较

    我的实验机器配置是: 处理器:Intel(R) Core(TM) i3-7100U CPU @ 2.40GHz 2.40GHz 随机访问存储器:4.00GB 操作系统:Windows10 集成开发环境 ...

  2. centos 下安装jdk、tomcat 以及tomcat无法从外部访问的解决办法

    centos 下安装jdk.tomcat 以及tomcat无法从外部访问的解决办法 原创 2014年08月28日 10:24:33 标签: selinux enforce cent 2223 昨天在c ...

  3. 将非常规Json字符串转换为常用的json对象

    如下所示,这是一个已经转换为Json对象的非常规Json字符串,原来是一个Json类型的字符串,在转换为Json对象时,查询资料发现有两种转换法,.parse()和.eval()方法,但是前辈们都极其 ...

  4. FLP不可能性(FLP impossibility)

    FLP不可能性(FLP impossibility) FLP impossibility是一个定理,它证明了在分布式情景下,无论任何算法,即使是只有一个进程挂掉,对于其他非失败进程,都存在着无法达成一 ...

  5. JavaScript 系统知识点图库

    JavaScript 是世界上最流行的,轻量级的编程语言. 这门语言可用于 HTML 和 web,更可广泛用于服务器.PC.笔记本电脑.平板电脑和智能手机等设备: JavaScript 被数百万计的网 ...

  6. hive Beeline plus HiveServer2简单使用

    HiveServer2是经常与beeline一起使用,可以用jdbc客户端远程连接,一般用于生产环境. 在提供传统客服端的功能之外,还提供其他功能: Beeline连接 1.先在hadoop集群启动H ...

  7. Python 简单理解多线程

    进程,是一个或多个线程的集合,每个进程在内存中是相对独立的. 线程,是计算机最小的运算单元,每个进程至少要有一个线程,多个线程时,每个线程间之间共享内存. 分别举例常规运行和多线程运行: 0)常规运行 ...

  8. MS08_067漏洞学习研究

    p197 MS08-067漏洞渗透攻击 按照书上的设置做,exploit得到错误信息: Exploit failed [unreachable]: Rex::ConnectionRefused The ...

  9. P1132 数字生成游戏

    题目请见:传送门 以下为题解,直接从洛谷上搬过来的,还专门改了markdown,(汗) 宽搜 with 一些技巧 由于查询量很大,所以要预先处理所有答案 预处理当然是用BFS,并同时进行delete, ...

  10. Mac OS X更新VirtualBox以后Genymotion无法启动的一种情况

    这两天VirtualBox更新到5.0了,于是乎就升级了.结果升级后就中了个大奖,Genymotion起不来了.我用的是Genymotion 2.5,不久前刚升级的,因为官网打不开,不知是不是最新版. ...