DisplayAttribute应用——根据PropertyName自动获取对应的UI显示名
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显示名的更多相关文章
- 转载-centos网络配置(手动设置,自动获取)的2种方法
转载地址:http://blog.51yip.com/linux/1120.html 重新启动网络配置 # service network restart 或 # /etc/init.d/networ ...
- 分享一个快速设置背景的js 自动获取背景图的长宽
我来分享一个快速设置背景的js (需要jq支持!) 快速切图铺页面用---就是不需要手动输入背景图的长宽 自动获取背景图的长宽 : <div class="wrap"> ...
- paip.enhes efis 自动获取文件的中文编码
paip.enhes efis 自动获取文件的中文编码 ##为什么需要自动获取文件的中文编码 提高开发效率,自动获取文件的中文编码 .不需要手动设置编码...轻松的.. ##cpdetector 可 ...
- 关于启明星系统移除apppath配置,让系统自动获取路径来设置cookie的解决方法
启明星系统底层使用统一接口,特别是用户,用户登录后,都会建立一个 userinfo 的cookie.请看下面2个网址: http://120.24.86.232/book http://120.24. ...
- ThinkPHP自动获取关键词(调用第三方插件)
ThinkPHP自动获取关键词调用在线discuz词库 先按照下图路径放好插件 方法如下 /** * 自动获取关键词(调用第三方插件) * @return [type] [description] * ...
- 基于jquery的表格动态创建,自动绑定,自动获取值
最近刚加入GUT项目,学习了很多其他同事写的代码,感觉受益匪浅. 在GUT项目中,经常会碰到这样一个问题:动态生成表格,包括从数据库中读取数据,并绑定在表格中,以及从在页面上通过jQuery新增删除表 ...
- I.MX6 Linux 自动获取AR1020 event input节点
/*********************************************************************** * I.MX6 Linux 自动获取AR1020 ev ...
- ARM-Linux配置DHCP自动获取IP地址
备注:内核版本:2.6.30.9busybox版本:1.15.2 PC Linux和开发板Linux的工作用户:root 1. 配置内核:[*] Networking support --->N ...
- Oracle VM Virtual 下CentOS不能自动获取IP地址
在CentOS配置网卡开机自动获取IP地址: vi /etc/sysconfig/network-scripts/ifcfg-eth0 将 ONBOOT="no" 改为 ONBOO ...
随机推荐
- 小技巧:with用法 pycharm控制台输出带颜色的文字 打印进度条的
with用法 with用法在python中是一个很独特的用法,因为别的语言的中没有这个用法.所以针对这个特点我们来做一次总结,什么样的情况下可以同with 我们学到的有文件的操作,和acquire ...
- python基础--数据类型、运算符、流程控制
原文地址:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143165862 ...
- 一份令人愉快的vs代码包和资源的整理清单
https://viatsko.github.io/awesome-vscode/ https://github.com/viatsko/awesome-vscode
- 【xsy1103】随机数表(RanMat)矩阵快速幂
题目大意:你生成了一个随机数表,生成机制是这样子的: $a[i]=A1a[i-1]+A2(2≤i≤m)$ $b[i]=B1b[i-1]+B2(2≤i≤m)$ $M[1][y]=a[y]%P,(1≤y≤ ...
- Selenium常用API的使用java语言之12-定位一组元素
在第(五)节我们已经学习了8种定位方法, 那8种定位方法是针对单个元素定位的, WebDriver还提供了另外8种用于定位一组元素的方法. import org.openqa.selenium.By; ...
- Vue Router 使用方法
安装 直接下载 / CDN https://unpkg.com/vue-router/dist/vue-router.js Unpkg.com 提供了基于 NPM 的 CDN 链接.上面的链接会一直指 ...
- BZOJ4802 欧拉函数 (Pollard-Rho Miller-Robin)
题目 求大数的欧拉函数φ\varphiφ 题解 Pollard-Rho 板子 CODE #pragma GCC optimize (3) #include <bits/stdc++.h> ...
- vue+大文件上传控件
总结一下大文件分片上传和断点续传的问题.因为文件过大(比如1G以上),必须要考虑上传过程网络中断的情况.http的网络请求中本身就已经具备了分片上传功能,当传输的文件比较大时,http协议自动会将文件 ...
- 如何安全的从LVM中移除磁盘
学习如何安全的从LVM中移除磁盘,当磁盘卷中有磁盘出现问题或是想把磁盘卷中的磁盘重新使用时就显得十分有用.本教程将重点关注以下问题: 如何安全的从LVM中移除磁盘 如何联机从VG中移除磁盘 如 ...
- TensorFlow(三):非线性回归
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # 非线性回归 # 使用numpy生成200个随机 ...