1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Reflection;
  5.  
  6. namespace Utils
  7. {
  8. /// <summary>
  9. /// 枚举帮助类
  10. /// </summary>
  11. public class EnumHelper
  12. {
  13. /// <summary>
  14. /// 返回枚举值的描述信息。
  15. /// </summary>
  16. /// <param name="value">要获取描述信息的枚举值。</param>
  17. /// <returns>枚举值的描述信息。</returns>
  18. public static string GetEnumDesc<T>(int value)
  19. {
  20. Type enumType = typeof(T);
  21. DescriptionAttribute attr = null;
  22.  
  23. // 获取枚举常数名称。
  24. string name = Enum.GetName(enumType, value);
  25. if (name != null)
  26. {
  27. // 获取枚举字段。
  28. FieldInfo fieldInfo = enumType.GetField(name);
  29. if (fieldInfo != null)
  30. {
  31. // 获取描述的属性。
  32. attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
  33. }
  34. }
  35.  
  36. // 返回结果
  37. if (attr != null && !string.IsNullOrEmpty(attr.Description))
  38. return attr.Description;
  39. else
  40. return string.Empty;
  41. }
  42.  
  43. /// <summary>
  44. /// 返回枚举项的描述信息。
  45. /// </summary>
  46. /// <param name="e">要获取描述信息的枚举项。</param>
  47. /// <returns>枚举项的描述信息。</returns>
  48. public static string GetEnumDesc(Enum e)
  49. {
  50. if (e == null)
  51. {
  52. return string.Empty;
  53. }
  54. Type enumType = e.GetType();
  55. DescriptionAttribute attr = null;
  56.  
  57. // 获取枚举字段。
  58. FieldInfo fieldInfo = enumType.GetField(e.ToString());
  59. if (fieldInfo != null)
  60. {
  61. // 获取描述的属性。
  62. attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
  63. }
  64.  
  65. // 返回结果
  66. if (attr != null && !string.IsNullOrEmpty(attr.Description))
  67. return attr.Description;
  68. else
  69. return string.Empty;
  70. }
  71.  
  72. /// <summary>
  73. /// 获取枚举描述列表,并转化为键值对
  74. /// </summary>
  75. /// <typeparam name="T"></typeparam>
  76. /// <param name="isHasAll">是否包含“全部”</param>
  77. /// <param name="filterItem">过滤项</param>
  78. /// <returns></returns>
  79. public static List<EnumKeyValue> EnumDescToList<T>(bool isHasAll, params string[] filterItem)
  80. {
  81. List<EnumKeyValue> list = new List<EnumKeyValue>();
  82.  
  83. // 如果包含全部则添加
  84. if (isHasAll)
  85. {
  86. list.Add(new EnumKeyValue() { Key = , Name = "全部" });
  87. }
  88.  
  89. #region 方式一
  90. foreach (var item in typeof(T).GetFields())
  91. {
  92. // 获取描述
  93. var attr = item.GetCustomAttribute(typeof(DescriptionAttribute), true) as DescriptionAttribute;
  94. if (attr != null && !string.IsNullOrEmpty(attr.Description))
  95. {
  96. // 跳过过滤项
  97. if (Array.IndexOf<string>(filterItem, attr.Description) != -)
  98. {
  99. continue;
  100. }
  101. // 添加
  102. EnumKeyValue model = new EnumKeyValue();
  103. model.Key = (int)Enum.Parse(typeof(T), item.Name);
  104. model.Name = attr.Description;
  105. list.Add(model);
  106. }
  107. }
  108. #endregion
  109.  
  110. #region 方式二
  111. //foreach (int item in Enum.GetValues(typeof(T)))
  112. //{
  113. // // 获取描述
  114. // FieldInfo fi = typeof(T).GetField(Enum.GetName(typeof(T), item));
  115. // var attr = fi.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
  116. // if (attr != null && !string.IsNullOrEmpty(attr.Description))
  117. // {
  118. // // 跳过过滤项
  119. // if (Array.IndexOf<string>(filterItem, attr.Description) != -1)
  120. // {
  121. // continue;
  122. // }
  123. // // 添加
  124. // EnumKeyValue model = new EnumKeyValue();
  125. // model.Key = item;
  126. // model.Name = attr.Description;
  127. // list.Add(model);
  128. // }
  129. //}
  130. #endregion
  131.  
  132. return list;
  133. }
  134.  
  135. /// <summary>
  136. /// 获取枚举值列表,并转化为键值对
  137. /// </summary>
  138. /// <typeparam name="T"></typeparam>
  139. /// <param name="isHasAll">是否包含“全部”</param>
  140. /// <param name="filterItem">过滤项</param>
  141. /// <returns></returns>
  142. public static List<EnumKeyValue> EnumToList<T>(bool isHasAll, params string[] filterItem)
  143. {
  144. List<EnumKeyValue> list = new List<EnumKeyValue>();
  145.  
  146. // 如果包含全部则添加
  147. if (isHasAll)
  148. {
  149. list.Add(new EnumKeyValue() { Key = , Name = "全部" });
  150. }
  151.  
  152. foreach (int item in Enum.GetValues(typeof(T)))
  153. {
  154. string name = Enum.GetName(typeof(T), item);
  155. // 跳过过滤项
  156. if (Array.IndexOf<string>(filterItem, name) != -)
  157. {
  158. continue;
  159. }
  160. // 添加
  161. EnumKeyValue model = new EnumKeyValue();
  162. model.Key = item;
  163. model.Name = name;
  164. list.Add(model);
  165. }
  166.  
  167. return list;
  168. }
  169. }
  170.  
  171. /// <summary>
  172. /// 枚举键值对
  173. /// </summary>
  174. public class EnumKeyValue
  175. {
  176. public int Key { get; set; }
  177. public string Name { get; set; }
  178. }
  179. }

C# EnumHelper的更多相关文章

  1. EnumHelper.cs枚举助手(枚举描述信息多语言支持)C#

    C#里面经常会用到枚举类型,枚举是值类型对象,如果你想用枚举类型的多属性特性,或者你想在MVC页面上通过简单的值类型转换,将某字段值所代表的含义转换为文字显示,这时候必须要将枚举扩展,是它支持文本描述 ...

  2. EnumHelper枚举常用操作类

    在项目中需要把枚举填充到下拉框中,所以使用统一的方法实现,测试代码如下: namespace CutPictureTest.Comm { public class EnumHelper { publi ...

  3. 在C#编程中玩转枚举,分享我的EnumHelper。

    在C#编程中玩转枚举,分享我的EnumHelper. 在软件开发过程中,我们经常会为特定的场景下的特定数据定义逻辑意义.比如在用户表中,我们可能会有一个用户状态字段,该字段为整形.如果该字段的值为1则 ...

  4. C# EnumHelper Enum的值,Description,ToString()的相互转换

    首先定义枚举类型,如下: /// <summary> /// 板块 /// </summary> public enum Plate {         [Descriptio ...

  5. EnumHelper.cs

    网上找的,还比较实用的: using System; using System.Collections.Generic; using System.ComponentModel; using Syst ...

  6. .net工具类

    ConvertHelper public class ConvertHelper { /// <summary> /// 转换类型 /// </summary> /// < ...

  7. c#枚举使用详解

    简介 1. 枚举(enum type)通常用来表示一组常量.由于枚举是强类型的,这在编程中给我们提供了极大的方便. 2. 枚举的定义: public enum Sex { 男 = 0, 女 = 1 } ...

  8. grape动态PHP结构(二)——管理后台

    一.概述

  9. 利用SCORE法则来总结一次偷懒的单元测试过程

    最近遇到一个单元测试的问题,本周正好学个了一个SCORE法则,这里正好练练手应用此法则将问题的前因后果分享给大家. S:背景  代码要有单元测试,检测的标准就是统计代码的单元测试覆盖率,程序员需要达到 ...

随机推荐

  1. 仿async/await(一)and Gulp:新一代前端构建利器

    NET 4.5的async/await真是个神奇的东西,巧妙异常以致我不禁对其实现充满好奇,但一直难以窥探其门径.不意间读了此篇强文<Asynchronous Programming in C# ...

  2. WCF Restful Service的服务

    构建基于WCF Restful Service的服务 前言 传统的Asmx服务,由于遵循SOAP协议,所以返回内容以xml方式组织.并且客户端需要添加服务端引用才能使用(虽然看到网络上已经提供了这方面 ...

  3. c/c++中typedef详解

    1. typedef 最简单使用 typedef long byte_4; // 给已知数据类型long起个新名字,叫byte_4 你可以在任何需要 long 的上下文中使用 byte_4.注意 ty ...

  4. TFS二次开发的数据统计以PBI、Bug、Sprint等为例(一)

    TFS二次开发的数据统计以PBI.Bug.Sprint等为例(一) 在TFS二次开发中,我们可能会根据某一些情况对各个项目的PBI.BUG等工作项进行统计.在本文中将大略讲解如果进行这些数据统计. 一 ...

  5. Microsoft Message Analyzer (微软消息分析器,“网络抓包工具 - Network Monitor”的替代品)官方正式版现已发布

    Microsoft Message Analyzer (微软消息分析器,“网络抓包工具 - Network Monitor”的替代品)官方正式版现已发布 来自官方日志的喜悦 被誉为全新开始的消息分析器 ...

  6. [转载]linux下编译php中configure参数具体含义

    编译N次了   原来这么回事 原文地址:linux下编译php中configure参数具体含义作者:捷心特 php编译参数的含义 ./configure –prefix=/usr/local/php ...

  7. OpenCV 4 Python高级配置—安装setuptools,matplotlib,six,dateutil,pyparsing 完整过程

    Matplotib 是python 的一个绘图库,里头有各种各样的绘图方法,可以用Matplotib 显示图像,放大图像,保存图像等等,对于OpenCV处理图像具有非常大的帮助.但是,安装Matplo ...

  8. Arduino 各种模块篇 GPRS module 手机模块 短信 电话 上网 for texting, calling, internet

    ---恢复内容开始--- The GPRS shield which I tested is one which looks like this: ---恢复内容结束--- Need to be re ...

  9. flask 真是太棒啦,阅读手册后就能做出一个博客了

    真是很好的东西,有很多有益处的东西. 有template引擎, 有flask自己带的g (用来处理访问与数据库打开关闭的) 有flask自己的处理session的功能 自带的jinja2模板引擎也是比 ...

  10. Spring.Net-DI依赖注入和Ioc控制反转

    Spring.Core作为整个Spring框架的基础,实现了依赖注入的功能.Spring框架的其它模块都要依赖或扩展该模块. IObjectFactory接口,该接口实现了工厂模式,使用它可以帮我们创 ...