///<summary> /// 根据枚举类型获取描述 ///</summary> ///<param name="value">类型</param> ///<returns>描述</returns> public static string GetEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return attributes[0].Description; } else { return value.ToString(); } }
例:
/// <summary> /// 认知分类(考核能力) /// </summary> public enum Perceiver { /// <summary> /// 识记 /// </summary> [Description("识记")] Memorize = 0, /// <summary> /// 理解 /// </summary> [Description("理解")] Understand = 1, /// <summary> /// 应用 /// </summary> [Description("应用")] Application = 2 }