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. panic: interface conversion: interface {} is nil, not chan *sarama.ProducerError

    使用golang kafka sarama 包时,遇到如下问题: 高并发情况下使用同步sync producer,偶尔遇到crash: panic: interface conversion: int ...

  2. HDU 5783 Divide the Sequence (训练题002 B)

    Description Alice has a sequence A, She wants to split A into as much as possible continuous subsequ ...

  3. easyui dialog 中嵌入html页面

    最近使用easyui比较多,这个插件确实很好用.在使用时也遇到了大大小小的问题,好在都一一解决了. 记录一下今天遇到的问题. 目的:用easyui的dialog嵌入一个html页面(html中仍有要执 ...

  4. HTML5新特性之WebRTC[转]

    原文:http://www.cnblogs.com/jscode/p/3601648.html?comefrom=http://blogread.cn/news/ 1.概述 WebRTC是“网络实时通 ...

  5. CSS 备忘

    border-radius :  10px  /  40px    10表示X轴半径   40表示Y轴半径   font:italic bold 13px/13px arial,sans-serif; ...

  6. HTML学习笔记 基础标签及css引用案例 第一节 (原创)参考使用表

    <!DOCTYPE html><!--头文件 不是标签 也没有结束,这是声明该文件为HTML5--><html lang="en"><!- ...

  7. jQuery_事件学习

    一.click事件 click事件----鼠标单击事件 $('.bt').click(function() { alert("本身的事件");}) 当class为bt的div被但单 ...

  8. Nginx实现负载均衡&Nginx缓存功能

    一.Nginx是什么 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambl ...

  9. Scrum Meeting Alpha - 7

    Scrum Meeting Alpha - 7 NewTeam 2017/11/1 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 登录部分的API仍存在问题 完成登陆部分的AP ...

  10. JAVA爬虫代码

    工程目录: 所需要的jar包为:   jsoup-1.10.2.jar /** * Created by wangzheng on 2017/2/19. */ public class Article ...