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

  

 

DisplayAttribute应用——根据PropertyName自动获取对应的UI显示名的更多相关文章

  1. 转载-centos网络配置(手动设置,自动获取)的2种方法

    转载地址:http://blog.51yip.com/linux/1120.html 重新启动网络配置 # service network restart 或 # /etc/init.d/networ ...

  2. 分享一个快速设置背景的js 自动获取背景图的长宽

    我来分享一个快速设置背景的js (需要jq支持!) 快速切图铺页面用---就是不需要手动输入背景图的长宽 自动获取背景图的长宽 : <div class="wrap"> ...

  3. paip.enhes efis 自动获取文件的中文编码

    paip.enhes efis 自动获取文件的中文编码 ##为什么需要自动获取文件的中文编码 提高开发效率,自动获取文件的中文编码  .不需要手动设置编码...轻松的.. ##cpdetector 可 ...

  4. 关于启明星系统移除apppath配置,让系统自动获取路径来设置cookie的解决方法

    启明星系统底层使用统一接口,特别是用户,用户登录后,都会建立一个 userinfo 的cookie.请看下面2个网址: http://120.24.86.232/book http://120.24. ...

  5. ThinkPHP自动获取关键词(调用第三方插件)

    ThinkPHP自动获取关键词调用在线discuz词库 先按照下图路径放好插件 方法如下 /** * 自动获取关键词(调用第三方插件) * @return [type] [description] * ...

  6. 基于jquery的表格动态创建,自动绑定,自动获取值

    最近刚加入GUT项目,学习了很多其他同事写的代码,感觉受益匪浅. 在GUT项目中,经常会碰到这样一个问题:动态生成表格,包括从数据库中读取数据,并绑定在表格中,以及从在页面上通过jQuery新增删除表 ...

  7. I.MX6 Linux 自动获取AR1020 event input节点

    /*********************************************************************** * I.MX6 Linux 自动获取AR1020 ev ...

  8. ARM-Linux配置DHCP自动获取IP地址

    备注:内核版本:2.6.30.9busybox版本:1.15.2 PC Linux和开发板Linux的工作用户:root 1. 配置内核:[*] Networking support --->N ...

  9. Oracle VM Virtual 下CentOS不能自动获取IP地址

    在CentOS配置网卡开机自动获取IP地址: vi /etc/sysconfig/network-scripts/ifcfg-eth0 将 ONBOOT="no" 改为 ONBOO ...

随机推荐

  1. linux fstab下挂载错误导致cannot open access to console, the root account is locked的问题

    用 deepin 安装 u 盘启动,出现选择安装语言的界面时,按 ctrl+alt+T,进入 tty,然后输入 startx,进入 live cd 模式,挂载硬盘的根分区,然后修改 /etc/fsta ...

  2. Python调用R编程——rpy2

    在Python调用R,最常见的方式是使用rpy2模块. 简介 模块 The package is made of several sub-packages or modules: rpy2.rinte ...

  3. 修改 hosts 完成域名绑定

    修改 hosts 完成域名绑定 mac 用户直接执行 vim /private/etc/hosts在 hosts 文件最后添加一行: 127.0.0.1a.com 这一句是什么意思呢? 就是告诉我们的 ...

  4. P2P system: Introduction

    P2P system : peer-to-peer system 一些流行的P2P system: Napster, Gnutella 我们为什么研究P2P system 大型的分布式系统有成千上万个 ...

  5. BZOJ2155(?) R集合 (卡特兰数)

    Orz Freopen大佬 CODE fac = [0]*1005 def C(n, m): return fac[n] // fac[m] // fac[n-m] n = int(input()) ...

  6. The 2019 China Collegiate Programming Contest Harbin Site K. Keeping Rabbits

    链接: https://codeforces.com/gym/102394/problem/K 题意: DreamGrid is the keeper of n rabbits. Initially, ...

  7. Fiddler抓包工具(捕获Android数据包)

    一:获取Android的数据包必须要在同一个网络中 移动设备访问网络原理 先看看移动设备是怎么去访问网络,如图所示,可以看到,移动端的数据包是从wifi出去的. 可以看得出,移动端的数据包,都是要走w ...

  8. 04_Tutorial 4: Authentication & Permissions 认证和权限

    1.认证和权限 0.文档 https://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/ https: ...

  9. 004_软件安装之_Altium Designer

    文件中有软件简单视频教程,安装有pdf教程 链接:https://pan.baidu.com/s/1ow-OHdsPuAyXCevjCVqEsg 提取码:l2rt 复制这段内容后打开百度网盘手机App ...

  10. 004_Python3 注释

    确保对模块, 函数, 方法和行内注释使用正确的风格 Python中的注释有单行注释和多行注释:Python中单行注释以 # 开头,例如::# 这是一个注释print("Hello, Worl ...