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 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("给定的类型不是枚举类型");
Dictionary 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)
{
FieldInfo[] enumItems = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
Dictionary 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("给定的类型不是枚举类型");
Dictionary 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)
{
FieldInfo[] enumItems = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
Dictionary values = new Dictionary(enumItems.Length);
foreach (FieldInfo 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);
if (_enumTypeDict.ContainsKey(typeName))
{
return _enumTypeDict[typeName];
}
return null;
}
private static ConcurrentDictionary LoadEnumTypeDict(Assembly assembly)
{
Type[] typeArray = assembly.GetTypes();
Dictionary dict = typeArray.Where(o => o.IsEnum).ToDictionary(o => o.Name, o => o);
return new ConcurrentDictionary(dict);
}
///
/// 根据枚举成员获取自定义属性EnumDisplayNameAttribute的属性DisplayName
///
///
public static Dictionary GetDescriptionAndValue(this Type enumType)
{
Dictionary dicResult = new Dictionary();
foreach (object e in Enum.GetValues(enumType))
{
dicResult.Add(GetDescription(e as Enum), (int)e);
}
return dicResult;
}
///
/// 根据枚举成员获取DescriptionAttribute的属性Description
///
///
public static string GetDescription(this Enum en)
{
Type type = en.GetType(); //获取类型
MemberInfo[] memberInfos = type.GetMember(en.ToString()); //获取成员
if (memberInfos.Any())
{
DescriptionAttribute[] attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; //获取描述特性
if (attrs != null && attrs.Length > 0)
{
return attrs[0].Description; //返回当前描述
}
}
return en.ToString();
}
///
/// 根据枚举成员获取Display的属性Name
///
///
public static string GetDisplay(this Enum en)
{
Type type = en.GetType(); //获取类型
MemberInfo[] memberInfos = type.GetMember(en.ToString()); //获取成员
if (memberInfos.Any())
{
DisplayAttribute[] attrs = memberInfos[0]?.GetCustomAttributes(typeof(DisplayAttribute), false) as DisplayAttribute[]; //获取描述特性
if (attrs != null && attrs.Length > 0)
{
return attrs[0].Name; //返回当前描述
}
}
return en.ToString();
}
///
/// 扩展方法:根据枚举值得到相应的枚举定义字符串
///
///
///
///
public static String ToEnumString(this int value, Type enumType)
{
NameValueCollection nvc = GetEnumStringFromEnumValue(enumType);
return nvc[value.ToString()];
}
///
/// 根据枚举类型得到其所有的 值 与 枚举定义字符串 的集合
///
///
///
public static NameValueCollection GetEnumStringFromEnumValue(Type enumType)
{
NameValueCollection nvc = new NameValueCollection();
FieldInfo[] 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;
}
}
}