EnumExtension.cs 7.0 KB

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