EnumExtension.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // 大名科技(天津)有限公司 版权所有
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
  6. //
  7. // 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
  8. namespace Admin.NET.Core;
  9. /// <summary>
  10. /// 枚举拓展
  11. /// </summary>
  12. public static class EnumExtension
  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;
  20. /// <summary>
  21. /// 获取枚举对象Key与名称的字典(缓存)
  22. /// </summary>
  23. /// <param name="enumType"></param>
  24. /// <returns></returns>
  25. public static Dictionary<int, string> GetEnumDictionary(this Type enumType)
  26. {
  27. if (!enumType.IsEnum)
  28. throw new ArgumentException("Type '" + enumType.Name + "' is not an enum.");
  29. // 查询缓存
  30. var enumDic = EnumNameValueDict.ContainsKey(enumType) ? EnumNameValueDict[enumType] : new Dictionary<int, string>();
  31. if (enumDic.Count != 0)
  32. return enumDic;
  33. // 取枚举类型的Key/Value字典集合
  34. enumDic = GetEnumDictionaryItems(enumType);
  35. // 缓存
  36. EnumNameValueDict[enumType] = enumDic;
  37. return enumDic;
  38. }
  39. /// <summary>
  40. /// 获取枚举对象Key与名称的字典
  41. /// </summary>
  42. /// <param name="enumType"></param>
  43. /// <returns></returns>
  44. private static Dictionary<int, string> GetEnumDictionaryItems(this Type enumType)
  45. {
  46. // 获取类型的字段,初始化一个有限长度的字典
  47. var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
  48. Dictionary<int, string> enumDic = new(enumFields.Length);
  49. // 遍历字段数组获取key和name
  50. foreach (var enumField in enumFields)
  51. {
  52. var intValue = (int)enumField.GetValue(enumType);
  53. enumDic[intValue] = enumField.Name;
  54. }
  55. return enumDic;
  56. }
  57. /// <summary>
  58. /// 获取枚举类型key与描述的字典(缓存)
  59. /// </summary>
  60. /// <param name="enumType"></param>
  61. /// <returns></returns>
  62. /// <exception cref="Exception"></exception>
  63. public static Dictionary<int, string> GetEnumDescDictionary(this Type enumType)
  64. {
  65. if (!enumType.IsEnum)
  66. throw new ArgumentException("Type '" + enumType.Name + "' is not an enum.");
  67. // 查询缓存
  68. var enumDic = EnumDisplayValueDict.ContainsKey(enumType)
  69. ? EnumDisplayValueDict[enumType]
  70. : new Dictionary<int, string>();
  71. if (enumDic.Count != 0)
  72. return enumDic;
  73. // 取枚举类型的Key/Value字典集合
  74. enumDic = GetEnumDescDictionaryItems(enumType);
  75. // 缓存
  76. EnumDisplayValueDict[enumType] = enumDic;
  77. return enumDic;
  78. }
  79. /// <summary>
  80. /// 获取枚举类型key与描述的字典(没有描述则获取name)
  81. /// </summary>
  82. /// <param name="enumType"></param>
  83. /// <returns></returns>
  84. /// <exception cref="Exception"></exception>
  85. private static Dictionary<int, string> GetEnumDescDictionaryItems(this Type enumType)
  86. {
  87. // 获取类型的字段,初始化一个有限长度的字典
  88. var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
  89. Dictionary<int, string> enumDic = new(enumFields.Length);
  90. // 遍历字段数组获取key和name
  91. foreach (var enumField in enumFields)
  92. {
  93. var intValue = (int)enumField.GetValue(enumType);
  94. var desc = enumField.GetDescriptionValue<DescriptionAttribute>();
  95. enumDic[intValue] = desc != null && !string.IsNullOrEmpty(desc.Description) ? desc.Description : enumField.Name;
  96. }
  97. return enumDic;
  98. }
  99. /// <summary>
  100. /// 从程序集中查找指定枚举类型
  101. /// </summary>
  102. /// <param name="assembly"></param>
  103. /// <param name="typeName"></param>
  104. /// <returns></returns>
  105. public static Type TryToGetEnumType(Assembly assembly, string typeName)
  106. {
  107. // 枚举缓存为空则重新加载枚举类型字典
  108. _enumTypeDict ??= LoadEnumTypeDict(assembly);
  109. // 按名称查找
  110. return _enumTypeDict.ContainsKey(typeName) ? _enumTypeDict[typeName] : null;
  111. }
  112. /// <summary>
  113. /// 从程序集中加载所有枚举类型
  114. /// </summary>
  115. /// <param name="assembly"></param>
  116. /// <returns></returns>
  117. private static ConcurrentDictionary<string, Type> LoadEnumTypeDict(Assembly assembly)
  118. {
  119. // 取程序集中所有类型
  120. var typeArray = assembly.GetTypes();
  121. // 过滤非枚举类型,转成字典格式并返回
  122. var dict = typeArray.Where(o => o.IsEnum).ToDictionary(o => o.Name, o => o);
  123. ConcurrentDictionary<string, Type> enumTypeDict = new(dict);
  124. return enumTypeDict;
  125. }
  126. /// <summary>
  127. /// 获取枚举的Description
  128. /// </summary>
  129. /// <param name="value"></param>
  130. /// <returns></returns>
  131. public static string GetDescription(this System.Enum value)
  132. {
  133. return value.GetType().GetMember(value.ToString()).FirstOrDefault()?.GetCustomAttribute<DescriptionAttribute>()
  134. ?.Description;
  135. }
  136. /// <summary>
  137. /// 获取枚举的Description
  138. /// </summary>
  139. /// <param name="value"></param>
  140. /// <returns></returns>
  141. public static string GetDescription(this object value)
  142. {
  143. return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault()
  144. ?.GetCustomAttribute<DescriptionAttribute>()?.Description;
  145. }
  146. /// <summary>
  147. /// 将枚举转成枚举信息集合
  148. /// </summary>
  149. /// <param name="type"></param>
  150. /// <returns></returns>
  151. public static List<EnumEntity> EnumToList(this Type type)
  152. {
  153. if (!type.IsEnum)
  154. throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
  155. var arr = System.Enum.GetNames(type);
  156. return arr.Select(sl =>
  157. {
  158. var item = System.Enum.Parse(type, sl);
  159. return new EnumEntity
  160. {
  161. Name = item.ToString(),
  162. Describe = item.GetDescription() ?? item.ToString(),
  163. Value = item.GetHashCode()
  164. };
  165. }).ToList();
  166. }
  167. /// <summary>
  168. /// 枚举ToList
  169. /// </summary>
  170. /// <typeparam name="T"></typeparam>
  171. /// <param name="type"></param>
  172. /// <returns></returns>
  173. public static List<T> EnumToList<T>(this Type type)
  174. {
  175. if (!type.IsEnum)
  176. throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
  177. var arr = System.Enum.GetNames(type);
  178. return arr.Select(name => (T)System.Enum.Parse(type, name)).ToList();
  179. }
  180. }
  181. /// <summary>
  182. /// 枚举实体
  183. /// </summary>
  184. public class EnumEntity
  185. {
  186. /// <summary>
  187. /// 枚举的描述
  188. /// </summary>
  189. public string Describe { set; get; }
  190. /// <summary>
  191. /// 枚举名称
  192. /// </summary>
  193. public string Name { set; get; }
  194. /// <summary>
  195. /// 枚举对象的值
  196. /// </summary>
  197. public int Value { set; get; }
  198. }