网上找的,还比较实用的:

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

EnumHelper.cs的更多相关文章

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

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

  2. 特性Atrribute和枚举

    特性的简单实用!特性存放在metedata里面,它离不开反射. Program.cs class Program { static void Main(string[] args) { Console ...

  3. “PMS-基础权限管理系统”实施某谱OA系统经验总结

    “PMS-基础权限管理系统”介绍 "PMS-基础权限管理系统"是我一直想做的一个产品,融合多年开发及维护管理系统的经验,参考了很多系统,精心研制而成. 可以做为毕业设计参考,新手学 ...

  4. .NET MVC EF框架数据库连接配置

    1:数据库的配置和连接 Web.config <connectionStrings> <add name="SQLConnectionString" connec ...

  5. ASP.NET MVC 5.0 参考源码索引

    http://www.projky.com/asp.netmvc/5.0/Microsoft/AspNet/Mvc/Facebook/FacebookAppSettingKeys.cs.htmlhtt ...

  6. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  7. Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结

    Atitit 软件架构方法的进化与演进cs bs soa roa  msa  attilax总结 1.1. 软件体系架构是沿着单机到 CS 架构,再到 BS 的三层架构甚至多层架构逐步发展过来的,关于 ...

  8. 从java文件和CS文件里查询方法使用次数工具

    前几天,领导让我找一下老系统(Java)里getRemoteUser方法都哪个文件用了,package是什么,方法被调用了多少次,当时因为着急,所以,直接人工找的,但是以后要是再出现,人工找就太讨厌了 ...

  9. 关于 WP 开发中.xaml 与.xaml.cs 的关系

    今天我们先来看一下在WP8.1开发中最长见到的几个文件之间的关系.比较论证,在看这个问题之前我们简单看看.NET平台其他两个不同的框架: Windows Forms 先看看Window Forms中的 ...

随机推荐

  1. Photon Server 实现注册与登录(一) --- Hibernate整合到项目中

    本系列实现目的:基于Photon Server实现注册于登录 一.拷贝Nbibernate项目的文件到MyGamerServer项目中. 二.数据库新建表,结构如下 三.修改文件名和配置 (1).将拷 ...

  2. Django新手入门必看

    pip install django==2.1.7 (现在Django3.0出来,推荐大家可以使用一下Django3.0) pip list查看

  3. AtCoder Beginner Contest 144 题解

    传送门 $cf$ 自闭了,打 $abc$ 散散心 A - 9x9 ...这个有什么好讲的吗,题目看懂就会做了 #include<iostream> #include<cstdio&g ...

  4. Go的包管理工具(一)

    在前面转载了系列文章:Golang 需要避免踩的 50 个坑,总得来说阅读量都挺大.今天这篇文章,咱们一起聊聊Go的依赖包管理工具. 背景 每一门语言都有其依赖的生态,当我们使用Java语言的时候,使 ...

  5. 基于APM实现RPC服务和消息队列的指定消费

    本文内容是基于公司现有框架整理的一篇专利文章.该框架包含完整的一套DevOps流程,包括工单系统(容器申请.服务部署等)\配置中心\路由配置中心\服务治理平台\消息治理平台\葛朗台(基于Docker+ ...

  6. .Net下二进制形式的文件存储与读取

    .Net下图片的常见存储与读取凡是有以下几种:存储图片:以二进制的形式存储图片时,要把数据库中的字段设置为Image数据类型(SQL Server),存储的数据是Byte[].1.参数是图片路径:返回 ...

  7. HTML练习一

    效果图 动态图 html代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  8. MySQL自测测试

    #建学生信息表student create table student ( sno varchar(20) not null primary key, sname varchar(20) not nu ...

  9. jvm常用命令

    jps // 查看Java进程ID和main方法类名 jstack <进程ID> // 查看该进程的所有栈信息 jstack -l <进程ID> // 查看该进程的所有栈信息, ...

  10. Stanford NLP 课程笔记之计算字符串距离

    在自然语言处理任务中,有时候需要计算两个字符串之间的相似度,也可以称作是两者之间的距离,用最小编辑距离表示. 最小编辑距离用{Insertion,Deletion,Substitution}这三种操作 ...