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. C++11新特性之operator "" xxx(const char *, size_t n)

    从C++11开始,我们可以使用以下形式通过常量字符串构造自定义类型, 比如: class Person { public: Person(const std::string& name): _ ...

  2. Jenkins构建自动化--实例一

    1.新建任务 2.配置任务 3.构建任务 5.构建完成后,可以点击任务名称查看结果

  3. Java锁--Semaphore

    转载请注明出处:http://www.cnblogs.com/skywang12345/p/3534050.html Semaphore简介 Semaphore是一个计数信号量,它的本质是一个&quo ...

  4. MLP多层感知机

    @author:wepon @blog:http://blog.csdn.net/u012162613/article/details/43221829 转载:http://blog.csdn.net ...

  5. Python Scrapy 爬虫框架实例

    之前有介绍 scrapy 的相关知识,但是没有介绍相关实例,在这里做个小例,供大家参考学习. 注:后续不强调python 版本,默认即为python3.x. 爬取目标 这里简单找一个图片网站,获取图片 ...

  6. sqlplus、exp、imp不是内部或外部命令,也不是可运行的程序

    oracle 想exp导出数据库,参考网上的作业,进入CMD命令,黑屏后输入命令出现EXP后出现EXP不能内部或外部命令. 网上找到下列解决方法 摘自:http://blog.csdn.net/suz ...

  7. Linux命令的详解

           cat /etc/passwd文件中的每个用户都有一个对应的记录行,记录着这个用户的一下基本属性.该文件对所有用户可读.               /etc/shadow  文件正如他 ...

  8. ElementUI 之 Message,自动弹出,信息不显示问题

    Vue.use() 这种写法可能会页面刷新时自动弹出 message,可将下图红框里换成 Vue.component(Message)

  9. 006——C#关闭窗口 添加FormClosing事件

    (一)参考文献:C# 添加FormClosing事件 (二)在  项目名.Designer.cs中的InitializeComponent()添加: this.FormClosing += new S ...

  10. [转]protobuf的编译安装

    github地址:https://github.com/google/protobuf支持多种语言,有多个语言的版本,本文采用的是在centos7下编译源码进行安装. github上有详细的安装说明: ...