using System; using System.Collections.Generic; using System.ComponentModel; using System.Reflection; using System.Text; namespace MES.Utility.Core { /// /// 枚举类型操作公共类。 /// public static class EnumHelper { /// /// 获取枚举所有成员名称。 /// /// 枚举类型 public static string[] GetNames() { return Enum.GetNames(typeof(T)); } /// /// 检测枚举是否包含指定成员。 /// /// 枚举类型 /// 成员名或成员值 public static bool IsDefined(this Enum value) { Type type = value.GetType(); return Enum.IsDefined(type, value); } /// /// 返回指定枚举类型的指定值的描述。 /// /// 枚举类型 /// 枚举值 /// public static string GetDescription(this Enum value) { try { Type type = value.GetType(); FieldInfo field = type.GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); return (attributes.Length > 0) ? attributes[0].Description : string.Empty; } catch { return string.Empty; } } } }