关键词搜索

源码搜索 ×
×

C# 枚举操作工具类

发布2015-01-09浏览2348次

详情内容

  1. <span style="font-size:14px;">using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.ComponentModel;
  7. using System.Collections;
  8. using System.Collections.Specialized;
  9. namespace JianKunKing.Common.EnumClass
  10. {
  11. /// <summary>
  12. /// 枚举操作类
  13. /// </summary>
  14. public static class EnumOperation
  15. {
  16. #region 从枚举中获取Description
  17. /// <summary>
  18. /// 从枚举中获取Description
  19. /// </summary>
  20. /// <param name="enumName">需要获取枚举描述的枚举</param>
  21. /// <returns>描述内容</returns>
  22. public static string GetDescription(Enum enumName)
  23. {
  24. try
  25. {
  26. string _description = string.Empty;
  27. FieldInfo _fieldInfo = enumName.GetType().GetField(enumName.ToString());
  28. DescriptionAttribute[] _attributes = GetDescriptAttr(_fieldInfo);
  29. if (_attributes != null && _attributes.Length > 0)
  30. {
  31. _description = _attributes[0].Description;
  32. }
  33. else
  34. {
  35. _description = enumName.ToString();
  36. }
  37. return _description;
  38. }
  39. catch (Exception)
  40. {
  41. throw;
  42. }
  43. }
  44. #endregion
  45. #region 获取字段Description(private)
  46. /// <summary>
  47. /// 获取字段Description
  48. /// </summary>
  49. /// <param name="fieldInfo">FieldInfo</param>
  50. /// <returns>DescriptionAttribute[] </returns>
  51. private static DescriptionAttribute[] GetDescriptAttr(this FieldInfo fieldInfo)
  52. {
  53. try
  54. {
  55. if (fieldInfo != null)
  56. {
  57. return (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
  58. }
  59. return null;
  60. }
  61. catch (Exception)
  62. {
  63. throw;
  64. }
  65. }
  66. #endregion
  67. #region 根据Description获取枚举定义字符串
  68. /// <summary>
  69. /// 根据Description获取枚举定义字符串
  70. /// </summary>
  71. /// <typeparam name="T">枚举类型</typeparam>
  72. /// <param name="description">枚举描述</param>
  73. /// <returns>枚举</returns>
  74. public static T GetEnumName<T>(string description)
  75. {
  76. Type _type = typeof(T);
  77. foreach (FieldInfo field in _type.GetFields())
  78. {
  79. DescriptionAttribute[] _curDesc = field.GetDescriptAttr();
  80. if (_curDesc != null && _curDesc.Length > 0)
  81. {
  82. if (_curDesc[0].Description == description)
  83. {
  84. return (T)field.GetValue(null);
  85. }
  86. }
  87. else
  88. {
  89. if (field.Name == description)
  90. {
  91. return (T)field.GetValue(null);
  92. }
  93. }
  94. }
  95. throw new ArgumentException(string.Format("{0} 未能找到对应的枚举.", description), "Description");
  96. }
  97. #endregion
  98. #region 将枚举转换为ArrayList
  99. /// <summary>
  100. /// 将枚举转换为ArrayList
  101. /// 说明:
  102. /// 若不是枚举类型,则返回NULL
  103. /// </summary>
  104. /// <param name="type">枚举类型</param>
  105. /// <returns>ArrayList</returns>
  106. public static ArrayList ToArrayList(Type type)
  107. {
  108. try
  109. {
  110. if (type.IsEnum)
  111. {
  112. ArrayList _array = new ArrayList();
  113. Array _enumValues = Enum.GetValues(type);
  114. foreach (Enum value in _enumValues)
  115. {
  116. _array.Add(new KeyValuePair<Enum, string>(value, GetDescription(value)));
  117. }
  118. return _array;
  119. }
  120. return null;
  121. }
  122. catch (Exception)
  123. {
  124. throw;
  125. }
  126. }
  127. #endregion
  128. #region 根据枚举值得到属性Description中的描述, 如果没有定义此属性则返回空串
  129. /// <summary>
  130. /// 根据枚举值得到属性Description中的描述, 如果没有定义此属性则返回空串
  131. /// </summary>
  132. /// <param name="value"></param>
  133. /// <param name="enumType"></param>
  134. /// <returns></returns>
  135. public static String GetEnumDescriptionString(int value, Type enumType)
  136. {
  137. try
  138. {
  139. NameValueCollection nvc = GetNVCFromEnumValue(enumType);
  140. return nvc[value.ToString()];
  141. }
  142. catch (Exception)
  143. {
  144. throw;
  145. }
  146. }
  147. #endregion
  148. #region 根据枚举类型得到其所有的值 与 枚举定义Description属性 的集合
  149. /// <summary>
  150. /// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合
  151. /// </summary>
  152. /// <param name="enumType"></param>
  153. /// <returns></returns>
  154. public static NameValueCollection GetNVCFromEnumValue(Type enumType)
  155. {
  156. try
  157. {
  158. NameValueCollection nvc = new NameValueCollection();
  159. Type typeDescription = typeof(DescriptionAttribute);
  160. System.Reflection.FieldInfo[] fields = enumType.GetFields();
  161. string strText = string.Empty;
  162. string strValue = string.Empty;
  163. foreach (FieldInfo field in fields)
  164. {
  165. if (field.FieldType.IsEnum)
  166. {
  167. strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
  168. object[] arr = field.GetCustomAttributes(typeDescription, true);
  169. if (arr.Length > 0)
  170. {
  171. DescriptionAttribute aa = (DescriptionAttribute)arr[0];
  172. strText = aa.Description;
  173. }
  174. else
  175. {
  176. strText = "";
  177. }
  178. nvc.Add(strValue, strText);
  179. }
  180. }
  181. return nvc;
  182. }
  183. catch (Exception)
  184. {
  185. throw;
  186. }
  187. }
  188. #endregion
  189. #region 根据枚举值得到相应的枚举定义字符串
  190. /// <summary>
  191. ///根据枚举值得到相应的枚举定义字符串
  192. /// </summary>
  193. /// <param name="value"></param>
  194. /// <param name="enumType"></param>
  195. /// <returns></returns>
  196. public static String GetEnumString(int value, Type enumType)
  197. {
  198. try
  199. {
  200. NameValueCollection nvc = GetEnumStringFromEnumValue(enumType);
  201. return nvc[value.ToString()];
  202. }
  203. catch (Exception)
  204. {
  205. throw;
  206. }
  207. }
  208. #endregion
  209. #region 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合
  210. /// <summary>
  211. /// 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合
  212. /// </summary>
  213. /// <param name="enumType"></param>
  214. /// <returns></returns>
  215. public static NameValueCollection GetEnumStringFromEnumValue(Type enumType)
  216. {
  217. try
  218. {
  219. NameValueCollection nvc = new NameValueCollection();
  220. Type typeDescription = typeof(DescriptionAttribute);
  221. System.Reflection.FieldInfo[] fields = enumType.GetFields();
  222. string strText = string.Empty;
  223. string strValue = string.Empty;
  224. foreach (FieldInfo field in fields)
  225. {
  226. if (field.FieldType.IsEnum)
  227. {
  228. strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
  229. nvc.Add(strValue, field.Name);
  230. }
  231. }
  232. return nvc;
  233. }
  234. catch (Exception)
  235. {
  236. throw;
  237. }
  238. }
  239. #endregion
  240. }
  241. }</span>


如果知道枚举的编号就是那个int类型的数字,获取name直接强转就可以 (枚举类型)(int值)


小注:
       对于基础公共方法类来说,其中最好不是获取异常直接抛出,切记不要吞掉或者只抛出部分信息。

异常部分可以参考:点击打开链接

本人参考:http://blog.csdn.net/yenange/article/details/7788332

     

相关技术文章

最新源码

下载排行榜

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载