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. 如何制作一个Arduino温度数据记录仪

    在本项目中,我们将使用Arduino开发板制作一个温度数据记录仪,该设备从温度传感器LM35获取温度值,并从DS3231实时时钟模块获取时间.然后我们将使用mini SD卡模块将这些值存储在SD卡文件 ...

  2. python在算法题中判断输入结束(EOF)

    有些算法题是这样要求的,一行输入一行数据,然后没有规定我要输入多少行,你要自行判断文件结束EOF,否则是会runtime error的,因为oj内部都是用文件来进行读写的. 例如a+b,每一行输入a ...

  3. html知识补充

    1.点击超链接跳转到新窗口 <a href="http://www.baidu.com" target="_blank">百度一下</a> ...

  4. 能ping通Linux但是ssh连不上问题解决方法

    问题:能ping通Linux服务器 但是ssh连不上  <Linux redhat AS4 版本> 解决方法这个问题花了我20分钟去查资料,网上写的解决方法也是五花八门,不过,总算解决了, ...

  5. 【xsy2978】Product of Roots 生成函数+多项式ln+多项式exp

    题目大意:给你两个多项式$f(x)$和$g(x)$,满足$f(x)=\prod\limits_{i=1}^{n}(a_i+1)$,$g(x)=\prod\limits_{i=1}^{m}(b_i+1) ...

  6. HDU-2082-找单词(母函数)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2082 题意: 假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1, ...

  7. Laravel Model updating&updated 事件使用注意事项

    1 触发条件 1.1 updating 1.1.1 如果字段无变化,不会触发此事件. 1.1.2 除非更改至少一个字段的值 2 事件逻辑不会覆盖 2.1 Trait 中定义事件如下 /** * The ...

  8. Greenplum 行存、列存,堆表、AO表的原理和选择

    转载自: https://github.com/digoal/blog/blob/master/201708/20170818_02.md?spm=a2c4e.11153940.blogcont179 ...

  9. springboot项目上传文件大小限制问题

    1.报错信息: Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors wi ...

  10. Educational Codeforces Round 70

    目录 Contest Info Solutions A. You Are Given Two Binary Strings... B. You Are Given a Decimal String.. ...