EnumHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace OASystem.Domain.Enums
  10. {
  11. public static class EnumHelper
  12. {
  13. //--extension
  14. /// <summary>
  15. /// 获取当前枚举描述
  16. /// </summary>
  17. /// <param name="enumValue"></param>
  18. /// <returns></returns>
  19. public static string GetEnumDescription(this Enum enumValue)
  20. {
  21. try
  22. {
  23. Type type = enumValue.GetType();
  24. MemberInfo[] memInfo = type.GetMember(enumValue.ToString());
  25. if (null != memInfo && memInfo.Length > 0)
  26. {
  27. object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
  28. if (null != attrs && attrs.Length > 0)
  29. return ((DescriptionAttribute)attrs[0]).Description;
  30. }
  31. return enumValue.ToString();
  32. }
  33. catch (Exception)
  34. {
  35. return "";
  36. }
  37. }
  38. //根据值获取枚举方法
  39. public static Enum GetEnumByValue(Type enumType, string value)
  40. {
  41. return Enum.Parse(enumType, value) as Enum;
  42. }
  43. /// <summary>
  44. /// 根据Key获取枚举描述
  45. /// </summary>
  46. /// <param name="en"></param>
  47. /// <returns></returns>
  48. public static string GetDescription(this System.Enum en)
  49. {
  50. Type type = en.GetType();
  51. MemberInfo[] memInfo = type.GetMember(en.ToString());
  52. if (memInfo != null && memInfo.Length > 0)
  53. {
  54. object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
  55. if (attrs != null && attrs.Length > 0)
  56. return ((DescriptionAttribute)attrs[0]).Description;
  57. }
  58. return en.ToString();
  59. }
  60. /// <summary>
  61. /// 根据Key获取枚举描述
  62. /// </summary>
  63. /// <param name="en"></param>
  64. /// <returns></returns>
  65. public static string GetDescriptionLabel(this System.Enum en)
  66. {
  67. Type type = en.GetType();
  68. MemberInfo[] memInfo = type.GetMember(en.ToString());
  69. if (memInfo != null && memInfo.Length > 0)
  70. {
  71. object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
  72. if (attrs != null && attrs.Length > 0)
  73. return ((DescriptionAttribute)attrs[0]).Description;
  74. }
  75. return en.ToString();
  76. }
  77. /// <summary>
  78. /// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合
  79. /// </summary>
  80. /// <param name="enumType"></param>
  81. /// <returns></returns>
  82. public static NameValueCollection GetNVCFromEnumValue(Type enumType)
  83. {
  84. NameValueCollection nvc = new NameValueCollection();
  85. Type typeDescription = typeof(DescriptionAttribute);
  86. System.Reflection.FieldInfo[] fields = enumType.GetFields();
  87. string strText = string.Empty;
  88. string strValue = string.Empty;
  89. foreach (FieldInfo field in fields)
  90. {
  91. if (field.FieldType.IsEnum)
  92. {
  93. //strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
  94. strValue = (enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null) as int?)?.ToString() ?? string.Empty;
  95. object[] arr = field.GetCustomAttributes(typeDescription, true);
  96. if (arr.Length > 0)
  97. {
  98. DescriptionAttribute aa = (DescriptionAttribute)arr[0];
  99. strText = aa.Description;
  100. }
  101. else
  102. {
  103. strText = "";
  104. }
  105. nvc.Add(strValue, strText);
  106. }
  107. }
  108. return nvc;
  109. }
  110. /// <summary>
  111. /// 获取枚举的所有值及其对应的描述
  112. /// </summary>
  113. /// <param name="enumType"></param>
  114. /// <param name="enumKeys"></param>
  115. /// <returns></returns>
  116. public static Dictionary<string, string> GetEnumDescriptions(Type enumType, string[]? enumKeys = null)
  117. {
  118. var descriptions = new Dictionary<string, string>();
  119. if (!enumType.IsEnum)
  120. {
  121. return descriptions;
  122. //throw new ArgumentException("Type must be an enum", nameof(enumType));
  123. }
  124. foreach (var field in enumType.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
  125. {
  126. var descriptionAttribute = field.GetCustomAttribute<DescriptionAttribute>();
  127. var description = descriptionAttribute?.Description ?? field.Name;
  128. var name = field.Name;
  129. if (enumKeys != null && enumKeys.Any())
  130. {
  131. if (!enumKeys.Contains(name))
  132. {
  133. descriptions[name] = description;
  134. }
  135. }
  136. else {
  137. descriptions[name] = description;
  138. }
  139. }
  140. return descriptions;
  141. }
  142. /// <summary>
  143. /// 获取枚举的所有项(值、名称、描述)
  144. /// </summary>
  145. public static List<EnumItem> GetEnumItems<T>() where T : Enum
  146. {
  147. var items = new List<EnumItem>();
  148. var type = typeof(T);
  149. var values = Enum.GetValues(type);
  150. foreach (Enum value in values)
  151. {
  152. var field = type.GetField(value.ToString());
  153. var description = field?.GetCustomAttribute<DescriptionAttribute>()?.Description;
  154. items.Add(new EnumItem
  155. {
  156. Value = Convert.ToInt32(value),
  157. Name = value.ToString(),
  158. Description = description ?? value.ToString()
  159. });
  160. }
  161. return items;
  162. }
  163. /// <summary>
  164. /// 将枚举转换为下拉列表数据源
  165. /// </summary>
  166. public static List<SelectListItem> ToSelectList<T>(bool addDefault = false, string defaultText = "请选择") where T : Enum
  167. {
  168. var list = new List<SelectListItem>();
  169. if (addDefault)
  170. {
  171. list.Add(new SelectListItem { Value = -1, Text = defaultText });
  172. }
  173. foreach (var item in GetEnumItems<T>())
  174. {
  175. list.Add(new SelectListItem
  176. {
  177. Value = item.Value,
  178. Text = item.Description
  179. });
  180. }
  181. return list;
  182. }
  183. // 辅助类
  184. public class EnumItem
  185. {
  186. public int Value { get; set; }
  187. public string Name { get; set; }
  188. public string Description { get; set; }
  189. }
  190. // MVC 中的 SelectListItem
  191. public class SelectListItem
  192. {
  193. public int Value { get; set; }
  194. public string Text { get; set; }
  195. }
  196. }
  197. }