获取枚举的描述
2016-08-30 00:00
197 查看
摘要: 通过反射的方式获取枚举元素的描述特性
引用
利用反射的方式,去获取枚举元素的特性
引用
System.ComponentModel为枚举中的元素设置Description特性
public enum PublishState { [Description("信息置顶")] Top = 1, [Description("信息审核")] Approve, [Description("信息反审核")] Disapprove, [Description("信息删除")] Remove }
利用反射的方式,去获取枚举元素的特性
public static string GetDescription(Enum en) { Type type = en.GetType(); MemberInfo[] memInfo = type.GetMember(en.ToString()); if (memInfo != null && memInfo.Length > 0) { object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false); if (attrs != null && attrs.Length > 0) { return ((DescriptionAttribute)attrs[0]).Description; } } return en.ToString(); }
相关文章推荐