Enum的简单扩展
1 添加一个描述的Attribute
public enum MessageResult
{
[System.ComponentModel.Description("未通过")]
UnPass = ,
[System.ComponentModel.Description("通过")]
Pass = ,
}
2 添加扩展方法
public static class EnumExtension
{
private static readonly ConcurrentDictionary<string, Dictionary<object, string>> EnumDescriptions
= new ConcurrentDictionary<string, Dictionary<object, string>>(); public static Dictionary<object, string> AsValueDesDic(this Type enumType, bool nameAsEmptyDes = false)
{
Trace.Assert(enumType.IsEnum);
Func<Type, Dictionary<object, string>> builder = type =>
{
var dic = new Dictionary<object, string>();
foreach (var i in Enum.GetValues(type))
{
var name = Enum.GetName(type, i);
var filed = type.GetField(name);
var attr = filed.GetCustomAttributes<System.ComponentModel.DescriptionAttribute>()
.FirstOrDefault();
if (attr != null)
dic.Add(i, attr.Description);
else if (nameAsEmptyDes)
dic.Add(i, name);
} return dic;
};
return EnumDescriptions.GetOrAdd(enumType.FullName, key => builder(enumType));
}
}
3 使用方式
var a = typeof(MessageResult).AsValueDesDic();
foreach (var item in a)
{
Trace.WriteLine((int)item.Key+":"+item.Value);
}
4 输出内容
Debug Trace:
:未通过
:通过
Enum的简单扩展的更多相关文章
- 对bootstrap modal的简单扩展封装
对bootstrap modal的简单扩展封装 参考自:http://www.muzilei.com/archives/677 注:原文不支持bootstrap新版本,并且居中等存在问题 此段时间 ...
- Log4net创建日志及简单扩展
转:http://blog.csdn.net/CHENFEIYANG2009/article/details/5397342 1.概述 log4net是.Net下一个非常优秀的开源日志记录组件.log ...
- 使用Log4net创建日志及简单扩展
如何使用Log4net创建日志及简单扩展 1.概述 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的 ...
- mysql中set和enum使用(简单介绍)
简单介绍 SET类型 在创建表时,就指定SET类型的取值范围. 属性名 SET('值1','值2','值3'...,'值n') 其中,“属性名”参数指字段的名称:“值n”参数表示列表中的第n个值,这些 ...
- 由一道面试题简单扩展 — setTimeout、闭包
在一个前端公众号,看到这么一个号称简单的面试题: 1.以下程序输出什么? <script type="text/javascript"> function init() ...
- RestSharp简单扩展
using RestSharp; using RestSharp.Deserializers; using RestSharp.Serializers; using System; using Sys ...
- 使用enum建立简单的状态机
Overview The enum in Java is more powerful than many other languages which can lead to surprising us ...
- Enum枚举 简单的使用
在枚举中使用抽象方法 /** * 为枚举类定义一个抽象方法,<br/> * 这个抽象方法由不同的枚举值提供不同的实现 * * @author wangzhu * @date 2014-9- ...
- Enum的简单了解
Enum可以将一组具名的有限集合创建成一种新的类型,而这些具名的值可以作为常规的程序组件使用. 在创建enum时,编译器会为你生成一个相关的类,这个类继承自java.lang.Enum,所以enum本 ...
随机推荐
- WPF 3D model - Sphere, Cone, and Cylinder
原文:WPF 3D model - Sphere, Cone, and Cylinder Extending Visual3D - Sphere, Cone, and Cylinder http: ...
- 【树转数组】poj1195
/* 二维的树状数组: 更新某个元素时: NO.1:c[n1],c[n2],c[n3],....,c[nm]; 当中n1 = i,n(i+1) = ni+lowbit(ni); nm+lowbit(n ...
- windows消息值全部定义,从消息值得到消息名称(系统消息定义从0到1023,从1024开始就是WM_USER,但是中间有325个WM_undefined消息,估计是备用,另外各控件都有一些reserved消息,也是为了备用)LostSpeed
前言 在逆向算法扫描插件时, 遇到一个windows消息值在msdn中没有定义. 去查资料, 有个老外将全部windows消息值和消息名称定义都码好了:) 写个测试程序, 封装了一个接口, 从消息值得 ...
- spring boot 集成mybatis连接oracle数据库
1. POM文件添加依赖 <!-- Mybatis --> <dependency> <groupId>org.mybatis.spring.boot</gr ...
- Servlet 3.1实践
Servlet 3.1 新特性详解 参考: IBM developerworks: Servlet 3.0 新特性详解 开涛的博客: Servlet3.1规范(最终版) 关键特性 Asynchroni ...
- 疯狂的图形(利用C# + GDI plus模拟杂乱无章的现实场景)
原文:疯狂的图形(利用C# + GDI plus模拟杂乱无章的现实场景) 本文给出了模拟竹叶.长叶草.杂乱石头.天上繁星等关键代码.使用.Net环境下C#语言,GDI+编写. 模拟竹叶 挺像的吧? ...
- asp .net 大文件传输配置
asp .net config配置 需要在配置文件里面设置文件上传限定的两个属性值:maxAllowedContentLength,maxRequestLength 允许上传文件的长度,和请求的长度, ...
- x:Static
用途:访问代码中的变量等 后台定义一个变量 public partial class GetStaticFromBackgroundCode : Window { public static stri ...
- Hermite曲线插值
原文 Hermite Curve Interpolation Hermite Curve Interpolation Hamburg (Germany), the 30th March 1998. W ...
- nodejs dateformat date-utils
https://www.npmjs.org/package/dateformat https://www.npmjs.org/package/date-utils 来自为知笔记(Wiz)