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

  

 

最新文章

  1. WUI 前端组件
  2. js动态的把左边列表添加到右边,可上下移动。
  3. 总结oninput、onchange与onpropertychange事件的用法和区别,onchange
  4. CSS总结1
  5. Asp.Net Web API 2第八课——Web API 2中的属性路由
  6. Freemarker-标签使用
  7. JavaScript : 基本的处理事件
  8. dataguard集群搭建
  9. java 写文本文件
  10. [译]使用Babel和Browserify创建你的ES6项目
  11. VMware: windows8 与 虚拟机ubuntu 14.04 共享文件夹
  12. centos 安装 vsftp
  13. 转:从pickle看python类成员的动态加载和类的定位
  14. ASP.NET MVC Boilerplate简介
  15. jQuery 事件——关于select选中
  16. 我的第一个python web开发框架(3)——怎么开始?
  17. JAVA_SE基础——25.面向对象练习
  18. (四十八)Quartz2D引擎进阶
  19. Unity 阴影的制作方式
  20. IntelliJ IDEA2017 使用教程

热门文章

  1. ES6 解构赋值详解
  2. PYTHON WEB开发学习路线
  3. 日常2018/4/9---b/s和c/s架构分别是什么?区别?
  4. 混合应用 微信登录授权 微信登录认证失败 ios PGWXAPI错误-1 code:-100 / 安卓 message:invalid appsecret innerCode:40125
  5. python练习题(三)
  6. JavaScript事件——拖拉事件
  7. 51 arm x86 的大小端记录
  8. BZOJ 1576 树剖+LCT
  9. TensorFlow(十五):使用inception-v3实现各种图像识别
  10. 学习C/C++的简单方法