EnumUtil.cs 6.9 KB

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