using System.ComponentModel; using System.Reflection; namespace Entity.Constant; public class BaseEnumExtensions { //根据枚举获取注释 public static string GetDescription(Enum value) { FieldInfo field = value.GetType().GetField(value.ToString()); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute == null ? value.ToString() : attribute.Description; } //根据code获取枚举 public static T GetEnumByCode(int code) where T : Enum { return (T)Enum.ToObject(typeof(T), code); } //根据code获取值 public static string GetEnumDescriptionByCode(int code) where T : Enum { return GetDescription((T)Enum.ToObject(typeof(T), code)); } }