[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}\tName:{item.Name}\tValue:{item.GetRawConstantValue()}"); } } Console.ReadKey(); }
枚举类:
////// 表示审批状态的枚举类 /// enum ApprovalStatus { ////// ? /// [Description("")] NoUse = 0, ////// 未提交 /// [Description("未提交")] NoSubmit = 1, ////// 审核中 /// [Description("审核中")] OnApprovaling = 2, ////// 待生效 /// [Description("待生效")] OnEffecting = 3, ////// 已生效 /// [Description("已生效")] Effected = 4, ////// 拒绝 /// [Description("拒绝")] Reject = 5, ////// 撤销 /// [Description("撤销")] Undo = 6 }
效果: