using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; namespace LifePayment.Domain.Shared; /// /// 枚举拓展 /// public static partial class EnumExtension { // 枚举显示字典缓存 private static readonly ConcurrentDictionary> EnumDisplayValueDict = new(); // 枚举值字典缓存 private static readonly ConcurrentDictionary> EnumNameValueDict = new(); // 枚举类型缓存 private static ConcurrentDictionary _enumTypeDict; /// /// 获取枚举对象Key与名称的字典(缓存) /// /// /// public static Dictionary GetEnumDictionary(this Type enumType) { if (!enumType.IsEnum) throw new ArgumentException("Type '" + enumType.Name + "' is not an enum."); // 查询缓存 var enumDic = EnumNameValueDict.ContainsKey(enumType) ? EnumNameValueDict[enumType] : new Dictionary(); if (enumDic.Count != 0) return enumDic; // 取枚举类型的Key/Value字典集合 enumDic = GetEnumDictionaryItems(enumType); // 缓存 EnumNameValueDict[enumType] = enumDic; return enumDic; } /// /// 获取枚举对象Key与名称的字典 /// /// /// private static Dictionary GetEnumDictionaryItems(this Type enumType) { // 获取类型的字段,初始化一个有限长度的字典 var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static); Dictionary enumDic = new(enumFields.Length); // 遍历字段数组获取key和name foreach (var enumField in enumFields) { var intValue = (int)enumField.GetValue(enumType); enumDic[intValue] = enumField.Name; } return enumDic; } /// /// 获取枚举类型key与描述的字典(缓存) /// /// /// /// public static Dictionary GetEnumDescDictionary(this Type enumType) { if (!enumType.IsEnum) throw new ArgumentException("Type '" + enumType.Name + "' is not an enum."); // 查询缓存 var enumDic = EnumDisplayValueDict.ContainsKey(enumType) ? EnumDisplayValueDict[enumType] : new Dictionary(); if (enumDic.Count != 0) return enumDic; // 取枚举类型的Key/Value字典集合 enumDic = GetEnumDescDictionaryItems(enumType); // 缓存 EnumDisplayValueDict[enumType] = enumDic; return enumDic; } /// /// 获取枚举类型key与描述的字典(没有描述则获取name) /// /// /// /// private static Dictionary GetEnumDescDictionaryItems(this Type enumType) { // 获取类型的字段,初始化一个有限长度的字典 var enumFields = enumType.GetFields(BindingFlags.Public | BindingFlags.Static); Dictionary enumDic = new(enumFields.Length); // 遍历字段数组获取key和name foreach (var enumField in enumFields) { var intValue = (int)enumField.GetValue(enumType); var desc = enumField.GetCustomAttribute(); enumDic[intValue] = desc != null && !string.IsNullOrEmpty(desc.Description) ? desc.Description : enumField.Name; } return enumDic; } /// /// 从程序集中查找指定枚举类型 /// /// /// /// public static Type TryToGetEnumType(Assembly assembly, string typeName) { // 枚举缓存为空则重新加载枚举类型字典 _enumTypeDict ??= LoadEnumTypeDict(assembly); // 按名称查找 return _enumTypeDict.ContainsKey(typeName) ? _enumTypeDict[typeName] : null; } /// /// 从程序集中加载所有枚举类型 /// /// /// private static ConcurrentDictionary LoadEnumTypeDict(Assembly assembly) { // 取程序集中所有类型 var typeArray = assembly.GetTypes(); // 过滤非枚举类型,转成字典格式并返回 var dict = typeArray.Where(o => o.IsEnum).ToDictionary(o => o.Name, o => o); ConcurrentDictionary enumTypeDict = new(dict); return enumTypeDict; } /// /// 获取枚举的Description /// /// /// public static string GetDescription(this T value) where T : Enum { return GetEnumDescDictionary(typeof(T))[value.GetHashCode()]; } /// /// 获取枚举的Description /// /// /// public static string GetDescription(this object value) { if (value==null) { return null; } return value.GetType().GetMember(value.ToString() ?? string.Empty).FirstOrDefault() ?.GetCustomAttribute()?.Description; } /// /// 将枚举转成枚举信息集合 /// /// /// public static List EnumToList(this Type type) { if (!type.IsEnum) throw new ArgumentException("Type '" + type.Name + "' is not an enum."); var arr = System.Enum.GetNames(type); return arr.Select(sl => { var item = System.Enum.Parse(type, sl); return new EnumEntity { Name = item.ToString(), Describe = item.GetDescription() ?? item.ToString(), Value = (int)item }; }).ToList(); } /// /// 将枚举转成枚举信息集合 /// /// /// public static List> EnumToList(this Type type) { if (!type.IsEnum) throw new ArgumentException("Type '" + type.Name + "' is not an enum."); var arr = System.Enum.GetNames(type); return arr.Select(sl => { var item = System.Enum.Parse(type, sl); return new EnumEntity { Name = item.ToString(), Describe = item.GetDescription() ?? item.ToString(), Value = (T)item }; }).ToList(); } /// /// 枚举ToList /// /// /// /// public static List EnumToEnumList(this Type type) { if (!type.IsEnum) throw new ArgumentException("Type '" + type.Name + "' is not an enum."); var arr = System.Enum.GetNames(type); return arr.Select(name => (T)System.Enum.Parse(type, name)).ToList(); } /// /// 是否是枚举中的值 /// /// /// public static bool IsEnumValue(this Enum value) { return Enum.IsDefined(value.GetType(), value); } } /// /// 枚举实体 /// public class EnumEntity { /// /// 枚举的描述 /// public string Describe { set; get; } /// /// 枚举名称 /// public string Name { set; get; } /// /// 枚举对象的值 /// public T Value { set; get; } } /// /// 枚举实体 /// public class EnumEntity : EnumEntity { } /// /// 包含枚举类型的枚举信息 /// public class EnumEntityAndEnumType { /// /// 枚举类型名称 /// public string EnumName { get; set; } /// /// 枚举值明细 /// public List> EnumEntitiy { get; set; } } /// /// 包含枚举类型的枚举信息 /// public class EnumEntityAndEnumType : EnumEntityAndEnumType { }