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();
private static NullableDictionary _enumTypeDict;
///
/// 获取枚举对象Key与显示名称的字典
///
///
///
public static Dictionary GetDictionary(this Type enumType)
{
if (!enumType.IsEnum)
{
throw new Exception("给定的类型不是枚举类型");
}
return EnumNameValueDict.GetOrAdd(enumType, _ => GetDictionaryItems(enumType));
}
private static Dictionary GetDictionaryItems(Type enumType)
{
var enumItems = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
var names = new Dictionary(enumItems.Length);
foreach (var enumItem in enumItems)
{
names[(int)enumItem.GetValue(enumType)] = enumItem.Name;
}
return names;
}
///
/// 获取枚举类型
///
///
///
///
public static Type GetEnumType(this Assembly assembly, string typeName)
{
_enumTypeDict ??= LoadEnumTypeDict(assembly);
return _enumTypeDict[typeName];
}
///
/// 根据枚举成员获取Display的属性Name
///
///
public static string GetDisplay(this Enum en)
{
var type = en.GetType(); //获取类型
var memberInfos = type.GetMember(en.ToString()); //获取成员
if (memberInfos.Any() && memberInfos[0].GetCustomAttributes(typeof(DisplayAttribute), false) is DisplayAttribute[] attrs && attrs.Length > 0)
{
return attrs[0].Name; //返回当前描述
}
return en.ToString();
}
private static NullableDictionary LoadEnumTypeDict(Assembly assembly)
{
return assembly.GetTypes().Where(o => o.IsEnum).ToDictionarySafety(o => o.Name, o => o);
}
///
/// 获取枚举值的Description信息
///
/// 枚举值
/// 要格式化的对象
/// 如果未找到DescriptionAttribute则返回null或返回类型描述
public static string GetDescription(this Enum value, params object[] args)
{
var type = value.GetType();
if (!Enum.IsDefined(type, value))
{
return Enum.GetValues(type).OfType().Where(value.HasFlag).Select(e =>
{
var member = type.GetField(e.ToString());
var description = member.GetCustomAttributes(typeof(DescriptionAttribute), false) is DescriptionAttribute[] attrs && attrs.Length != 0 ? attrs[0].Description : e.ToString();
return args.Length > 0 ? string.Format(description, args) : description;
}).Join(",");
}
var member = type.GetField(value.ToString());
var description = member.GetCustomAttributes(typeof(DescriptionAttribute), false) is DescriptionAttribute[] attributes && attributes.Length != 0 ? attributes[0].Description : value.ToString();
return args.Length > 0 ? string.Format(description, args) : description;
}
///
/// 获取枚举值的Description信息
///
/// 枚举值
/// 要格式化的对象
/// 如果未找到DescriptionAttribute则返回null或返回类型描述
public static IEnumerable GetAttributes(this Enum value) where TAttribute : Attribute
{
var type = value.GetType();
if (!Enum.IsDefined(type, value))
{
return Enum.GetValues(type).OfType().Where(value.HasFlag).SelectMany(e => type.GetField(e.ToString()).GetCustomAttributes(false));
}
return type.GetField(value.ToString()).GetCustomAttributes(false);
}
///
/// 拆分枚举值
///
/// 枚举值
public static IEnumerable Split(this TEnum value) where TEnum : Enum
{
var type = typeof(TEnum);
return Enum.IsDefined(type, value) ? new[]
{
value
} : Enum.GetValues(type).Cast().Where(e => value.HasFlag(e));
}
///
/// 获取枚举值的Description信息
///
/// 枚举值
/// 如果未找到DescriptionAttribute则返回null或返回类型描述
public static EnumDescriptionAttribute GetEnumDescription(this Enum value)
{
return GetEnumDescriptions(value).FirstOrDefault();
}
///
/// 获取枚举值的Description信息
///
/// 枚举值
/// 如果未找到DescriptionAttribute则返回null或返回类型描述
public static NullableDictionary GetTypedEnumDescriptions(this Enum value)
{
return GetEnumDescriptions(value).ToDictionarySafety(a => a.Language, a => (a.Description, a.Display));
}
///
/// 获取枚举值的Description信息
///
/// 枚举值
/// 如果未找到DescriptionAttribute则返回null或返回类型描述
public static IEnumerable GetEnumDescriptions(this Enum value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
var type = value.GetType();
if (!Enum.IsDefined(type, value))
{
return Enum.GetValues(type).OfType().Where(value.HasFlag).SelectMany(e => type.GetField(e.ToString()).GetCustomAttributes(typeof(EnumDescriptionAttribute), false).OfType());
}
return type.GetField(value.ToString()).GetCustomAttributes(typeof(EnumDescriptionAttribute), false).OfType();
}
///
/// 扩展方法:根据枚举值得到相应的枚举定义字符串
///
///
///
///
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;
}
///
/// 根据枚举成员获取自定义属性EnumDisplayNameAttribute的属性DisplayName
///
public static Dictionary GetDescriptionAndValue(this Type enumType) => Enum.GetValues(enumType).Cast