[Obsolete("请使用新的方法XXX")] //使用Obsolete特性来告诉使用者这是一个过期的方法 private static void Test() { Type t = typeof(ApprovalStatus); var fields = t.GetFields(); foreach (var item in fields) { if (!item.IsSpecialName) { var attr = item.GetCustomAttribute(typeof(DescriptionAttribute), false); string attribute = ""; if (attr != null) attribute = ((DescriptionAttribute)attr).Description; Console.WriteLine($"Description:{attribute} Name:{item.Name} Value:{item.GetRawConstantValue()}"); } } Console.ReadKey(); }
枚举类:
/// <summary> /// 表示审批状态的枚举类 /// </summary> enum ApprovalStatus { /// <summary> /// ? /// </summary> [Description("")] NoUse = 0, /// <summary> /// 未提交 /// </summary> [Description("未提交")] NoSubmit = 1, /// <summary> /// 审核中 /// </summary> [Description("审核中")] OnApprovaling = 2, /// <summary> /// 待生效 /// </summary> [Description("待生效")] OnEffecting = 3, /// <summary> /// 已生效 /// </summary> [Description("已生效")] Effected = 4, /// <summary> /// 拒绝 /// </summary> [Description("拒绝")] Reject = 5, /// <summary> /// 撤销 /// </summary> [Description("撤销")] Undo = 6 }
效果: