EnumExtension.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /// 将枚举转成枚举信息集合
  146. /// </summary>
  147. /// <param name="type"></param>
  148. /// <returns></returns>
  149. public static List<EnumEntity> EnumToList(this Type type)
  150. {
  151. if (!type.IsEnum)
  152. throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
  153. var arr = System.Enum.GetNames(type);
  154. return arr.Select(sl =>
  155. {
  156. var item = System.Enum.Parse(type, sl);
  157. return new EnumEntity
  158. {
  159. Name = item.ToString(),
  160. Describe = item.GetDescription() ?? item.ToString(),
  161. Value = item.GetHashCode()
  162. };
  163. }).ToList();
  164. }
  165. /// <summary>
  166. /// 枚举ToList
  167. /// </summary>
  168. /// <typeparam name="T"></typeparam>
  169. /// <param name="type"></param>
  170. /// <returns></returns>
  171. public static List<T> EnumToList<T>(this Type type)
  172. {
  173. if (!type.IsEnum)
  174. throw new ArgumentException("Type '" + type.Name + "' is not an enum.");
  175. var arr = System.Enum.GetNames(type);
  176. return arr.Select(name => (T)System.Enum.Parse(type, name)).ToList();
  177. }
  178. }
  179. /// <summary>
  180. /// 枚举实体
  181. /// </summary>
  182. public class EnumEntity
  183. {
  184. /// <summary>
  185. /// 枚举的描述
  186. /// </summary>
  187. public string Describe { set; get; }
  188. /// <summary>
  189. /// 枚举名称
  190. /// </summary>
  191. public string Name { set; get; }
  192. /// <summary>
  193. /// 枚举对象的值
  194. /// </summary>
  195. public int Value { set; get; }
  196. }