EnumExtension.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System.Collections.Concurrent;
  2. namespace Admin.NET.Core;
  3. /// <summary>
  4. /// 枚举拓展
  5. /// </summary>
  6. public static class EnumExtension
  7. {
  8. // 枚举显示字典缓存
  9. private static readonly ConcurrentDictionary<Type, Dictionary<int, string>> EnumDisplayValueDict = new();
  10. // 枚举值字典缓存
  11. private static readonly ConcurrentDictionary<Type, Dictionary<int, string>> EnumNameValueDict = new();
  12. // 枚举类型缓存
  13. private static ConcurrentDictionary<string, Type> _enumTypeDict;
  14. /// <summary>
  15. /// 获取枚举对象Key与名称的字典(缓存)
  16. /// </summary>
  17. /// <param name="enumType"></param>
  18. /// <returns></returns>
  19. public static Dictionary<int, string> GetEnumDictionary(this Type enumType)
  20. {
  21. if (!enumType.IsEnum)
  22. throw new ArgumentException("Type '" + enumType.Name + "' is not an enum.");
  23. // 查询缓存
  24. var enumDic = EnumNameValueDict.ContainsKey(enumType) ? EnumNameValueDict[enumType] : new Dictionary<int, string>();
  25. if (enumDic.Count != 0)
  26. return enumDic;
  27. // 取枚举类型的Key/Value字典集合
  28. enumDic = GetEnumDictionaryItems(enumType);
  29. // 缓存
  30. EnumNameValueDict[enumType] = enumDic;
  31. return enumDic;
  32. }
  33. /// <summary>
  34. /// 获取枚举对象Key与名称的字典
  35. /// </summary>
  36. /// <param name="enumType"></param>
  37. /// <returns></returns>
  38. private static Dictionary<int, string> GetEnumDictionaryItems(this Type enumType)
  39. {
  40. // 获取类型的字段,初始化一个有限长度的字典
  41. var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
  42. Dictionary<int, string> enumDic = new(enumFields.Length);
  43. // 遍历字段数组获取key和name
  44. foreach (var enumField in enumFields)
  45. {
  46. var intValue = (int)enumField.GetValue(enumType);
  47. enumDic[intValue] = enumField.Name;
  48. }
  49. return enumDic;
  50. }
  51. /// <summary>
  52. /// 获取枚举类型key与描述的字典(缓存)
  53. /// </summary>
  54. /// <param name="enumType"></param>
  55. /// <returns></returns>
  56. /// <exception cref="Exception"></exception>
  57. public static Dictionary<int, string> GetEnumDescDictionary(this Type enumType)
  58. {
  59. if (!enumType.IsEnum)
  60. throw new ArgumentException("Type '" + enumType.Name + "' is not an enum.");
  61. // 查询缓存
  62. var enumDic = EnumDisplayValueDict.ContainsKey(enumType)
  63. ? EnumDisplayValueDict[enumType]
  64. : new Dictionary<int, string>();
  65. if (enumDic.Count != 0)
  66. return enumDic;
  67. // 取枚举类型的Key/Value字典集合
  68. enumDic = GetEnumDescDictionaryItems(enumType);
  69. // 缓存
  70. EnumDisplayValueDict[enumType] = enumDic;
  71. return enumDic;
  72. }
  73. /// <summary>
  74. /// 获取枚举类型key与描述的字典(没有描述则获取name)
  75. /// </summary>
  76. /// <param name="enumType"></param>
  77. /// <returns></returns>
  78. /// <exception cref="Exception"></exception>
  79. private static Dictionary<int, string> GetEnumDescDictionaryItems(this Type enumType)
  80. {
  81. // 获取类型的字段,初始化一个有限长度的字典
  82. var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
  83. Dictionary<int, string> enumDic = new(enumFields.Length);
  84. // 遍历字段数组获取key和name
  85. foreach (var enumField in enumFields)
  86. {
  87. var intValue = (int)enumField.GetValue(enumType);
  88. var desc = enumField.GetDescriptionValue<DescriptionAttribute>();
  89. enumDic[intValue] = desc != null && !string.IsNullOrEmpty(desc.Description) ? desc.Description : enumField.Name;
  90. }
  91. return enumDic;
  92. }
  93. /// <summary>
  94. /// 从程序集中查找指定枚举类型
  95. /// </summary>
  96. /// <param name="assembly"></param>
  97. /// <param name="typeName"></param>
  98. /// <returns></returns>
  99. public static Type TryToGetEnumType(Assembly assembly, string typeName)
  100. {
  101. // 枚举缓存为空则重新加载枚举类型字典
  102. _enumTypeDict ??= LoadEnumTypeDict(assembly);
  103. // 按名称查找
  104. return _enumTypeDict.ContainsKey(typeName) ? _enumTypeDict[typeName] : null;
  105. }
  106. /// <summary>
  107. /// 从程序集中加载所有枚举类型
  108. /// </summary>
  109. /// <param name="assembly"></param>
  110. /// <returns></returns>
  111. private static ConcurrentDictionary<string, Type> LoadEnumTypeDict(Assembly assembly)
  112. {
  113. // 取程序集中所有类型
  114. var typeArray = assembly.GetTypes();
  115. // 过滤非枚举类型,转成字典格式并返回
  116. var dict = typeArray.Where(o => o.IsEnum).ToDictionary(o => o.Name, o => o);
  117. ConcurrentDictionary<string, Type> enumTypeDict = new(dict);
  118. return enumTypeDict;
  119. }
  120. /// <summary>
  121. /// 获取枚举的Description
  122. /// </summary>
  123. /// <param name="value"></param>
  124. /// <returns></returns>
  125. public static string GetDescription(this System.Enum value)
  126. {
  127. return value.GetType().GetMember(value.ToString()).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>()
  128. ?.Description;
  129. }
  130. /// <summary>
  131. /// 获取枚举的Description
  132. /// </summary>
  133. /// <param name="value"></param>
  134. /// <returns></returns>
  135. public static string GetDescription(this object value)
  136. {
  137. return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()
  138. ?.GetCustomAttribute<DescriptionAttribute>()?.Description;
  139. }
  140. /// <summary>
  141. /// 将枚举转成枚举信息集合
  142. /// </summary>
  143. /// <param name="type"></param>
  144. /// <returns></returns>
  145. public static List<EnumEntity> EnumToList(this Type type)
  146. {
  147. if (!type.IsEnum)
  148. throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
  149. var arr = System.Enum.GetNames(type);
  150. return arr.Select(sl =>
  151. {
  152. var item = System.Enum.Parse(type, sl);
  153. return new EnumEntity
  154. {
  155. Name = item.ToString(),
  156. Describe = item.GetDescription(),
  157. Value = item.GetHashCode()
  158. };
  159. }).ToList();
  160. }
  161. /// <summary>
  162. /// 枚举ToList
  163. /// </summary>
  164. /// <typeparam name="T"></typeparam>
  165. /// <param name="type"></param>
  166. /// <returns></returns>
  167. public static List<T> EnumToList<T>(this Type type)
  168. {
  169. if (!type.IsEnum)
  170. throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
  171. var arr = System.Enum.GetNames(type);
  172. return arr.Select(name => (T)System.Enum.Parse(type, name)).ToList();
  173. }
  174. }
  175. /// <summary>
  176. /// 枚举实体
  177. /// </summary>
  178. public class EnumEntity
  179. {
  180. /// <summary>
  181. /// 枚举的描述
  182. /// </summary>
  183. public string Describe { set; get; }
  184. /// <summary>
  185. /// 枚举名称
  186. /// </summary>
  187. public string Name { set; get; }
  188. /// <summary>
  189. /// 枚举对象的值
  190. /// </summary>
  191. public int Value { set; get; }
  192. }