using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using MyCode.Project.Infrastructure.Extensions; namespace MyCode.Project.Infrastructure.Common { public class EnumHelper { #region Parse(获取实例) /// /// 获取实例 /// /// 枚举类型 /// 成员名或值,范例:Enum1枚举有成员A=0,则传入"A"或"0"获取 Enum1.A /// public static TEnum Parse(object member) { string value = member.SafeString(); if (string.IsNullOrEmpty(value)) { if (typeof(TEnum).IsGenericType) { return default(TEnum); } throw new ArgumentNullException(nameof(member)); } return (TEnum)System.Enum.Parse(Reflection.GetType(), value, true); } #endregion #region GetName(获取成员名) /// /// 获取成员名 /// /// 枚举类型 /// 成员名、值、实例均可,范例:Enum1枚举有成员A=0,则传入Enum1.A或0,获取成员名"A" /// public static string GetName(object member) { return GetName(Reflection.GetType(), member); } /// /// 获取成员名 /// /// 枚举类型 /// 成员名、值、实例均可,范例:Enum1枚举有成员A=0,则传入Enum1.A或0,获取成员名"A" /// public static string GetName(Type type, object member) { if (type == null) { return string.Empty; } if (member == null) { return string.Empty; } if (member is string) { return member.ToString(); } if (type.GetTypeInfo().IsEnum == false) { return string.Empty; } return System.Enum.GetName(type, member); } #endregion #region GetNames(获取枚举所有成员名称) /// /// 获取枚举所有成员名称 /// /// 枚举类型 /// public static string[] GetNames() { return GetNames(typeof(TEnum)); } /// /// 获取枚举所有成员名称 /// /// 枚举类型 /// public static string[] GetNames(Type type) { return System.Enum.GetNames(type); } #endregion #region GetValue(获取成员值) /// /// 获取成员值 /// /// 枚举类型 /// 成员名、值、实例均可,范例:Enum1枚举有成员A=0,可传入"A"、0、Enum1.A,获取值0 /// public static int GetValue(object member) { return GetValue(Reflection.GetType(), member); } /// /// 获取成员值 /// /// 枚举类型 /// 成员名、值、实例均可,范例:Enum1枚举有成员A=0,可传入"A"、0、Enum1.A,获取值0 /// public static int GetValue(Type type, object member) { string value = member.SafeString(); if (string.IsNullOrEmpty(value)) { throw new ArgumentNullException(nameof(member)); } return (int)System.Enum.Parse(type, member.ToString(), true); } #endregion #region GetDescription(获取描述) /// /// 获取描述,使用 特性设置描述 /// /// 枚举 /// < param name="member">成员名、值、实例均可,范例:Enum1枚举有成员A=0,可传入"A"、0、Enum1.A,获取值0 /// public static string GetDescription(object member) { return Reflection.GetDescription(GetName(member)); } ///// ///// 获取描述,使用特性设置描述 ///// ///// 枚举类型 ///// 成员名、值、实例均可,范例:Enum1枚举有成员A=0,可传入"A"、0、Enum1.A,获取值0 ///// //public static string GetDescription(Type type, object member) //{ // return Reflection.GetDescription(type, GetName(type, member)); //} /// /// 取得描述 /// /// /// public static string GetDescription(Enum value) { Type type = value.GetType(); FieldInfo item = type.GetField(value.ToString(), BindingFlags.Public | BindingFlags.Static); if (item == null) return null; var attribute = Attribute.GetCustomAttribute(item, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute != null && !string.IsNullOrEmpty(attribute.Description)) return attribute.Description; return null; } #endregion #region GetItems(获取描述项集合) /// /// 获取描述项集合,文本设置为Description,值为Value /// /// 枚举类型 /// public static List GetItems() { return GetItems(Reflection.GetType()); } /// /// 获取描述项集合,文本设置为Description,值为Value /// /// 枚举类型 /// public static List GetItems(Type type) { TypeInfo enumType = type.GetTypeInfo(); if (enumType.IsEnum == false) { throw new InvalidOperationException($"类型 {type} 不是枚举"); } var result = new List(); foreach (var field in enumType.GetFields()) { AddItem(type, result, field); } return result.OrderBy(t => t.SortId).ToList(); } /// /// 验证是否枚举类型 /// /// 类型 private static void ValidateEnum(Type enumType) { if (enumType.IsEnum == false) { throw new InvalidOperationException(string.Format("类型 {0} 不是枚举", enumType)); } } /// /// 添加描述项 /// /// 枚举类型 /// 集合 /// 字段 private static void AddItem(Type type, ICollection result, FieldInfo field) { if (!field.FieldType.GetTypeInfo().IsEnum) { return; } var value = GetValue(type, field.Name); var description = Reflection.GetDescription(field); result.Add(new Item(description, value, value)); } #endregion #region GetEnumItemByDescription(获取指定描述信息的枚举项) /// /// 获取指定描述信息的枚举项 /// /// 枚举类型 /// 枚举项描述信息 /// public static TEnum GetEnumItemByDescription(string desc) { if (string.IsNullOrEmpty(desc)) { throw new ArgumentNullException(nameof(desc)); } Type type = typeof(TEnum); FieldInfo[] fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static); FieldInfo fieldInfo = fieldInfos.FirstOrDefault(p => p.GetCustomAttribute(false).Description == desc); if (fieldInfo == null) { throw new ArgumentNullException($"在枚举({type.FullName})中,未发现描述为“{desc}”的枚举项。"); } return (TEnum)System.Enum.Parse(type, fieldInfo.Name); } #endregion #region GetDictionary(获取枚举字典) /// /// 获取枚举字典 /// /// 枚举类型 /// public static Dictionary GetDictionary() { Type enumType = Reflection.GetType().GetTypeInfo(); ValidateEnum(enumType); Dictionary dic = new Dictionary(); foreach (var field in enumType.GetFields()) { AddItem(dic, field); } return dic; } /// /// 添加描述项 /// /// 枚举类型 /// 集合 /// 字典 private static void AddItem(Dictionary result, FieldInfo field) { if (!field.FieldType.GetTypeInfo().IsEnum) { return; } var value = GetValue(field.Name); var description = Reflection.GetDescription(field); result.Add(value, description); } #endregion } }