EnumExt.cs 7.3 KB

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