EnumExt.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.ComponentModel;
  6. using System.ComponentModel.DataAnnotations;
  7. using System.Linq;
  8. using System.Reflection;
  9. namespace Masuit.Tools.Systems
  10. {
  11. /// <summary>
  12. /// 枚举扩展类
  13. /// </summary>
  14. public static class EnumExt
  15. {
  16. private static readonly ConcurrentDictionary<Type, Dictionary<int, string>> EnumNameValueDict = new ConcurrentDictionary<Type, Dictionary<int, string>>();
  17. private static readonly ConcurrentDictionary<Type, Dictionary<string, int>> EnumValueNameDict = new ConcurrentDictionary<Type, Dictionary<string, int>>();
  18. private static ConcurrentDictionary<string, Type> _enumTypeDict;
  19. /// <summary>
  20. /// 获取枚举对象Key与显示名称的字典
  21. /// </summary>
  22. /// <param name="enumType"></param>
  23. /// <returns></returns>
  24. public static Dictionary<int, string> GetDictionary(this Type enumType)
  25. {
  26. if (!enumType.IsEnum) throw new Exception("给定的类型不是枚举类型");
  27. Dictionary<int, string> names = EnumNameValueDict.ContainsKey(enumType) ? EnumNameValueDict[enumType] : new Dictionary<int, string>();
  28. if (names.Count == 0)
  29. {
  30. names = GetDictionaryItems(enumType);
  31. EnumNameValueDict[enumType] = names;
  32. }
  33. return names;
  34. }
  35. private static Dictionary<int, string> GetDictionaryItems(Type enumType)
  36. {
  37. FieldInfo[] enumItems = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
  38. Dictionary<int, string> names = new Dictionary<int, string>(enumItems.Length);
  39. foreach (FieldInfo enumItem in enumItems)
  40. {
  41. int intValue = (int)enumItem.GetValue(enumType);
  42. names[intValue] = enumItem.Name;
  43. }
  44. return names;
  45. }
  46. /// <summary>
  47. /// 获取枚举对象显示名称与Key的字典
  48. /// </summary>
  49. /// <param name="enumType"></param>
  50. /// <returns></returns>
  51. public static Dictionary<string, int> GetValueItems(this Type enumType)
  52. {
  53. if (!enumType.IsEnum) throw new Exception("给定的类型不是枚举类型");
  54. Dictionary<string, int> values = EnumValueNameDict.ContainsKey(enumType) ? EnumValueNameDict[enumType] : new Dictionary<string, int>();
  55. if (values.Count == 0)
  56. {
  57. values = GetValueNameItems(enumType);
  58. EnumValueNameDict[enumType] = values;
  59. }
  60. return values;
  61. }
  62. private static Dictionary<string, int> GetValueNameItems(Type enumType)
  63. {
  64. FieldInfo[] enumItems = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
  65. Dictionary<string, int> values = new Dictionary<string, int>(enumItems.Length);
  66. foreach (FieldInfo enumItem in enumItems)
  67. {
  68. values[enumItem.Name] = (int)enumItem.GetValue(enumType);
  69. }
  70. return values;
  71. }
  72. /// <summary>
  73. /// 获取枚举对象的值内容
  74. /// </summary>
  75. /// <param name="enumType"></param>
  76. /// <param name="name"></param>
  77. /// <returns></returns>
  78. public static int GetValue(this Type enumType, string name)
  79. {
  80. if (!enumType.IsEnum) throw new Exception("给定的类型不是枚举类型");
  81. Dictionary<string, int> enumDict = GetValueNameItems(enumType);
  82. return enumDict.ContainsKey(name) ? enumDict[name] : enumDict.Select(d => d.Value).FirstOrDefault();
  83. }
  84. /// <summary>
  85. /// 获取枚举类型
  86. /// </summary>
  87. /// <param name="assembly"></param>
  88. /// <param name="typeName"></param>
  89. /// <returns></returns>
  90. public static Type GetEnumType(Assembly assembly, string typeName)
  91. {
  92. _enumTypeDict ??= LoadEnumTypeDict(assembly);
  93. if (_enumTypeDict.ContainsKey(typeName))
  94. {
  95. return _enumTypeDict[typeName];
  96. }
  97. return null;
  98. }
  99. private static ConcurrentDictionary<string, Type> LoadEnumTypeDict(Assembly assembly)
  100. {
  101. Type[] typeArray = assembly.GetTypes();
  102. Dictionary<string, Type> dict = typeArray.Where(o => o.IsEnum).ToDictionary(o => o.Name, o => o);
  103. return new ConcurrentDictionary<string, Type>(dict);
  104. }
  105. /// <summary>
  106. /// 根据枚举成员获取自定义属性EnumDisplayNameAttribute的属性DisplayName
  107. /// </summary>
  108. /// <returns></returns>
  109. public static Dictionary<string, int> GetDescriptionAndValue(this Type enumType)
  110. {
  111. Dictionary<string, int> dicResult = new Dictionary<string, int>();
  112. foreach (object e in Enum.GetValues(enumType))
  113. {
  114. dicResult.Add(GetDescription(e as Enum), (int)e);
  115. }
  116. return dicResult;
  117. }
  118. /// <summary>
  119. /// 根据枚举成员获取DescriptionAttribute的属性Description
  120. /// </summary>
  121. /// <returns></returns>
  122. public static string GetDescription(this Enum en)
  123. {
  124. Type type = en.GetType(); //获取类型
  125. MemberInfo[] memberInfos = type.GetMember(en.ToString()); //获取成员
  126. if (memberInfos.Any())
  127. {
  128. DescriptionAttribute[] attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; //获取描述特性
  129. if (attrs != null && attrs.Length > 0)
  130. {
  131. return attrs[0].Description; //返回当前描述
  132. }
  133. }
  134. return en.ToString();
  135. }
  136. /// <summary>
  137. /// 根据枚举成员获取Display的属性Name
  138. /// </summary>
  139. /// <returns></returns>
  140. public static string GetDisplay(this Enum en)
  141. {
  142. Type type = en.GetType(); //获取类型
  143. MemberInfo[] memberInfos = type.GetMember(en.ToString()); //获取成员
  144. if (memberInfos.Any())
  145. {
  146. DisplayAttribute[] attrs = memberInfos[0]?.GetCustomAttributes(typeof(DisplayAttribute), false) as DisplayAttribute[]; //获取描述特性
  147. if (attrs != null && attrs.Length > 0)
  148. {
  149. return attrs[0].Name; //返回当前描述
  150. }
  151. }
  152. return en.ToString();
  153. }
  154. /// <summary>
  155. /// 扩展方法:根据枚举值得到相应的枚举定义字符串
  156. /// </summary>
  157. /// <param name="value"></param>
  158. /// <param name="enumType"></param>
  159. /// <returns></returns>
  160. public static String ToEnumString(this int value, Type enumType)
  161. {
  162. NameValueCollection nvc = GetEnumStringFromEnumValue(enumType);
  163. return nvc[value.ToString()];
  164. }
  165. /// <summary>
  166. /// 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合
  167. /// </summary>
  168. /// <param name="enumType"></param>
  169. /// <returns></returns>
  170. public static NameValueCollection GetEnumStringFromEnumValue(Type enumType)
  171. {
  172. NameValueCollection nvc = new NameValueCollection();
  173. FieldInfo[] fields = enumType.GetFields();
  174. foreach (FieldInfo field in fields)
  175. {
  176. if (field.FieldType.IsEnum)
  177. {
  178. var strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
  179. nvc.Add(strValue, field.Name);
  180. }
  181. }
  182. return nvc;
  183. }
  184. }
  185. }