EnumDesc方法
using System; using System.ComponentModel; public string EnumDesc(Type type,int value) { string name = Enum.GetName(type, value); if (name == null) { return string.Empty; } FieldInfo field = type.GetField(name); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute == null ) { return name; } return attribute == null ? string.Empty : attribute.Description; }
调用时:
public enum enum123 { [Description("@a")] 枚举a = 0, [Description("@b")] 枚举b = 1, [Description("@c")] 枚举c = 2 } EnumDesc(typeof(enum123), 0);