using System; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace OASystem.Domain.Enums { public static class EnumHelper { //--extension /// /// 获取当前枚举描述 /// /// /// public static string GetEnumDescription(this Enum enumValue) { try { Type type = enumValue.GetType(); MemberInfo[] memInfo = type.GetMember(enumValue.ToString()); if (null != memInfo && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); if (null != attrs && attrs.Length > 0) return ((DescriptionAttribute)attrs[0]).Description; } return enumValue.ToString(); } catch (Exception) { return ""; } } //根据值获取枚举方法 public static Enum GetEnumByValue(Type enumType, string value) { return Enum.Parse(enumType, value) as Enum; } /// /// 根据Key获取枚举描述 /// /// /// public static string GetDescription(this System.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(); } /// /// 根据枚举类型得到其所有的 值 与 枚举定义Description属性 的集合 /// /// /// public static NameValueCollection GetNVCFromEnumValue(Type enumType) { NameValueCollection nvc = new NameValueCollection(); Type typeDescription = typeof(DescriptionAttribute); System.Reflection.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 = ""; } nvc.Add(strValue, strText); } } return nvc; } } }