using System.Collections.Concurrent;
using System.ComponentModel;
using System.Reflection;
namespace JNPF.Extras.CollectiveOAuth.Enums;
///
/// 枚举拓展类.
///
public static class EnumExtensions
{
private static ConcurrentDictionary _concurrentDictionary = new ConcurrentDictionary();
private static ConcurrentDictionary> _concurrentDicDictionary = new ConcurrentDictionary>();
///
/// 锁对象.
///
private static object objLock = new object();
///
/// 获取枚举的Code信息(Code).
///
public static int GetCode(this Enum @this)
{
return Convert.ToInt32(@this);
}
///
/// 获取枚举的描述信息(Descripion).
/// 支持位域,如果是位域组合值,多个按分隔符组合.
///
public static string GetDesc(this Enum @this)
{
return _concurrentDictionary.GetOrAdd(@this, (key) =>
{
var type = key.GetType();
var field = type.GetField(key.ToString());
// 如果field为null则应该是组合位域值,
return field == null ? key.GetDescriptions() : GetDescription(field);
});
}
///
/// 获取枚举的说明.
///
/// 枚举对象.
/// 位枚举的分割符号(仅对位枚举有作用).
public static string GetDescriptions(this Enum em, string split = ",")
{
var names = em.ToString().Split(',');
string[] res = new string[names.Length];
var type = em.GetType();
for (int i = 0; i < names.Length; i++)
{
var field = type.GetField(names[i].Trim());
if (field == null) continue;
res[i] = GetDescription(field);
}
return string.Join(split, res);
}
private static string GetDescription(FieldInfo field)
{
var att = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute), false);
return att == null ? field.Name : ((DescriptionAttribute)att).Description;
}
///
/// 把枚举转换成为列表.
///
public static List ToList(this Type type)
{
List list = new List();
foreach (object obj in Enum.GetValues(type))
{
list.Add(new EnumObject((Enum)obj));
}
return list;
}
///
/// 构造UTable枚举json样式 eg.{"Resource":{"value":0,"name":"Resource","text":"自有资源"},"New":{"value":1,"name":"New","text":"业务费用"}}.
///
///
///
public static Dictionary GetEnumList(this Type type)
{
Dictionary list = new Dictionary();
foreach (object obj in Enum.GetValues(type))
{
list.Add(((Enum)obj).ToString(), new EnumModel((Enum)obj));
}
return list;
}
///
/// 获取枚举值+描述.
///
/// Type,该参数的格式为typeof(需要读的枚举类型).
/// 键值对.
public static Dictionary GetEnumItemValueDesc(Type enumType)
{
Dictionary dic = new Dictionary();
Type typeDescription = typeof(DescriptionAttribute);
FieldInfo[] fields = enumType.GetFields();
string strText = string.Empty;
string strValue = string.Empty;
foreach (FieldInfo field in fields)
{
if (field.FieldType.IsEnum)
{
strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
object[] arr = field.GetCustomAttributes(typeDescription, true);
if (arr.Length > 0)
{
DescriptionAttribute aa = (DescriptionAttribute)arr[0];
strText = aa.Description;
}
else
{
strText = field.Name;
}
dic.Add(strValue, strText);
}
}
return dic;
}
///
/// 获取枚举类型键值对.
///
///
///
public static Dictionary GetEunItemValueAndDesc(Type em)
{
return _concurrentDicDictionary.GetOrAdd(em, (key) =>
{
var type = key.GetType();
if (_concurrentDicDictionary.ContainsKey(key))
return _concurrentDicDictionary[key];
else
return GetEnumItemValueDesc(em);
});
}
///
/// 获取枚举项+描述.
///
/// Type,该参数的格式为typeof(需要读的枚举类型).
/// 键值对.
public static Dictionary GetEnumItemDesc(Type enumType)
{
Dictionary dic = new Dictionary();
FieldInfo[] fieldinfos = enumType.GetFields();
foreach (FieldInfo field in fieldinfos)
{
if (field.FieldType.IsEnum)
{
object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
dic.Add(field.Name, ((DescriptionAttribute)objs[0]).Description);
}
}
return dic;
}
///
/// 获取枚举项描述信息 例如GetEnumDesc(Days.Sunday)
///
/// 枚举项 如Days.Sunday
///
public static string GetEnumDesc(this Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
return ((DescriptionAttribute)attrs[0]).Description;
}
return en.ToString();
}
///
/// 将注释转换成枚举值,匹配不上返回Null.
///
///
///
///
public static int? GetEnumValByDescription(this Type type, string strDescription)
{
int? enumVal = null;
foreach (object obj in Enum.GetValues(type))
{
Enum nEnum = (Enum)obj;
if (nEnum.GetDesc() == strDescription)
{
enumVal = (int)Convert.ChangeType(nEnum, typeof(int));
}
}
return enumVal;
}
}