EnumExtension.cs 8.1 KB

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