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