using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; namespace Masuit.Tools.Systems { /// /// 枚举扩展类 /// public static partial class EnumExt { private static readonly ConcurrentDictionary> EnumNameValueDict = new ConcurrentDictionary>(); private static readonly ConcurrentDictionary> EnumValueNameDict = new ConcurrentDictionary>(); private static ConcurrentDictionary _enumTypeDict; /// /// 获取枚举对象Key与显示名称的字典 /// /// /// public static Dictionary GetDictionary(this Type enumType) { if (!enumType.IsEnum) { throw new Exception("给定的类型不是枚举类型"); } var names = EnumNameValueDict.ContainsKey(enumType) ? EnumNameValueDict[enumType] : new Dictionary(); if (names.Count == 0) { names = GetDictionaryItems(enumType); EnumNameValueDict[enumType] = names; } return names; } private static Dictionary GetDictionaryItems(Type enumType) { var enumItems = enumType.GetFields(BindingFlags.Public | BindingFlags.Static); var names = new Dictionary(enumItems.Length); foreach (FieldInfo enumItem in enumItems) { int intValue = (int)enumItem.GetValue(enumType); names[intValue] = enumItem.Name; } return names; } /// /// 获取枚举对象显示名称与Key的字典 /// /// /// public static Dictionary GetValueItems(this Type enumType) { if (!enumType.IsEnum) { throw new Exception("给定的类型不是枚举类型"); } var values = EnumValueNameDict.ContainsKey(enumType) ? EnumValueNameDict[enumType] : new Dictionary(); if (values.Count == 0) { values = GetValueNameItems(enumType); EnumValueNameDict[enumType] = values; } return values; } private static Dictionary GetValueNameItems(Type enumType) { var enumItems = enumType.GetFields(BindingFlags.Public | BindingFlags.Static); var values = new Dictionary(enumItems.Length); foreach (var enumItem in enumItems) { values[enumItem.Name] = (int)enumItem.GetValue(enumType); } return values; } /// /// 获取枚举对象的值内容 /// /// /// /// public static int GetValue(this Type enumType, string name) { if (!enumType.IsEnum) { throw new Exception("给定的类型不是枚举类型"); } Dictionary enumDict = GetValueNameItems(enumType); return enumDict.ContainsKey(name) ? enumDict[name] : enumDict.Select(d => d.Value).FirstOrDefault(); } /// /// 获取枚举类型 /// /// /// /// public static Type GetEnumType(Assembly assembly, string typeName) { _enumTypeDict ??= LoadEnumTypeDict(assembly); return _enumTypeDict.ContainsKey(typeName) ? _enumTypeDict[typeName] : null; } /// /// 根据枚举成员获取Display的属性Name /// /// public static string GetDisplay(this Enum en) { var type = en.GetType(); //获取类型 var memberInfos = type.GetMember(en.ToString()); //获取成员 if (memberInfos.Any()) { if (memberInfos[0].GetCustomAttributes(typeof(DisplayAttribute), false) is DisplayAttribute[] attrs && attrs.Length > 0) { return attrs[0].Name; //返回当前描述 } } return en.ToString(); } private static ConcurrentDictionary LoadEnumTypeDict(Assembly assembly) { return new ConcurrentDictionary(assembly.GetTypes().Where(o => o.IsEnum).ToDictionary(o => o.Name, o => o)); } /// /// 根据枚举成员获取自定义属性EnumDisplayNameAttribute的属性DisplayName /// /// public static Dictionary GetDescriptionAndValue(this Type enumType) { return Enum.GetValues(enumType).Cast().ToDictionary(e => GetDescription(e as Enum), e => (int)e); } /// /// 根据枚举成员获取DescriptionAttribute的属性Description /// /// public static string GetDescription(this Enum en) { var type = en.GetType(); //获取类型 var memberInfos = type.GetMember(en.ToString()); //获取成员 if (memberInfos.Any()) { if (memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) is DescriptionAttribute[] attrs && attrs.Length > 0) { return attrs[0].Description; //返回当前描述 } } return en.ToString(); } /// /// 扩展方法:根据枚举值得到相应的枚举定义字符串 /// /// /// /// public static String ToEnumString(this int value, Type enumType) { return GetEnumStringFromEnumValue(enumType)[value.ToString()]; } /// /// 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合 /// /// /// public static NameValueCollection GetEnumStringFromEnumValue(Type enumType) { var nvc = new NameValueCollection(); var fields = enumType.GetFields(); foreach (FieldInfo field in fields) { if (field.FieldType.IsEnum) { var strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString(); nvc.Add(strValue, field.Name); } } return nvc; } } }