using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace HH.WMS.Entitys.Enums { public static class EnumExtensions { /// /// 获取特性 (DescriptionAttribute) 的说明;如果未使用该特性,则返回枚举的名称。 /// /// /// public static string GetDescription(this Enum enumValue) { FieldInfo fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); DescriptionAttribute[] attrs = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; return attrs.Length > 0 ? attrs[0].Description : enumValue.ToString(); } public static string GetName(this Enum enumValue) { return enumValue.ToString(); } public static T GetEnumByName(string name) { try { name = name.Trim(); var enumValue = (T)Enum.Parse(typeof(T), name); return enumValue; }catch(Exception){ return (T)Enum.Parse(typeof(T), "ERROR"); ; } } } }