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. Matplotlib常用示例入门

    一.Matplotlib介绍 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.通过 Matplotlib,开发者可以仅需要几行 ...

  2. Leetcode题解(十九)

    54.Spiral Matrix 题目: 题目意思很简单,就是螺旋式访问矩阵元素.也没有比较经典的算法可以解决此题,只需要模拟一下这个过程即可. 代码如下: class Solution { publ ...

  3. 链表倒数第n个节点

    找到单链表倒数第n个节点,保证链表中节点的最少数量为n. 样例 给出链表 3->2->1->5->null和n = 2,返回倒数第二个节点的值1. /** * Definiti ...

  4. Python 读取某个目录下的文件

    读取某个目录下的文件,如'/Users/test/test_kmls'目录下有test1.txt.test2.txt. 第一种方法读出的all_files是test1.txt.test2.txt im ...

  5. SVG绘制loading效果

    <div class="loading"> <svg width='40px' height='40px' xmlns="http://www.w3.o ...

  6. SeaJS之shim插件:解决非cmd规范的插件与sea的区别

    SeaJS 中的模块默认都遵守 CMD 规范,但现实中已存在大量普通 JavaScript 类库,比如 jQuery.Underscore 等.使用 shim 插件,可以将这些普通 JS 文件转换成 ...

  7. Jquery基础笔记

    1.$(function(){               等价于     window.onload=function(){ })                               } 2 ...

  8. asp.net core 开发的https证书服务-agilelabs.net

    创建证书-生成CSR(Certificate Sign Request): 填写证书基本信息 接下来我们就可以看到创建的证书签名请求信息(CSR): 为我们刚才创建的CSR签名: 签名的意思是说通过证 ...

  9. git 在linux下服务端搭建

    本文以centos为例,其他linux请自行参照对应方式. 1. 服务端安装git yum install git 2. 服务端添加无shell登录权限的用户,将username替换为要添加的用户 u ...

  10. 如何实现websocket服务器-理论篇

    WebSocket 服务器简单来说就是一个遵循特殊协议监听服务器任意端口的tcp应用.搭建一个定制服务器的任务通常会让让人们感到害怕.然而基于实现一个简单的Websocket服务器没有那么麻烦. 一个 ...