博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 给枚举类型增加一个备注特性
阅读量:6910 次
发布时间:2019-06-27

本文共 1510 字,大约阅读时间需要 5 分钟。

///     /// 备注特性    ///     public class RemarkAttribute : Attribute    {        ///         /// 备注        ///         public string Remark { get; set; }        public RemarkAttribute(string remark)        {            this.Remark = remark;        }    }
///     /// 枚举扩展类    ///     public static class EnumExtension    {        ///         /// 获取枚举的备注信息        ///         ///         /// 
public static string GetRemark(this Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); if (fi == null) { return value.ToString(); } object[] attributes = fi.GetCustomAttributes(typeof(RemarkAttribute), false); if (attributes.Length > 0) { return ((RemarkAttribute)attributes[0]).Remark; } else { return value.ToString(); } } public static string GetEnumDescription(this Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes.Length > 0) { return attributes[0].Description; } else { return value.ToString(); } } }
var aaa = UserType.Type1.GetRemark();            var aab = UserType.Type2.GetEnumDescription();

 

转载地址:http://oxycl.baihongyu.com/

你可能感兴趣的文章
微软职位内部推荐-Senior SDE
查看>>
Java Bigdecimal使用
查看>>
RabbitMQ三种Exchange模式(fanout,direct,topic)的性能比较
查看>>
[UI]抽屉菜单DrawerLayout分析(三)
查看>>
linux shell基础
查看>>
Android中的Animation 动画开发
查看>>
KVM 基础使用(一)
查看>>
怎么将Emeditor设置成网页查看源代码的默认编译器
查看>>
higncharts 编辑Highcharts.com链接
查看>>
ThinkPHP 模板显示display和assign的用法
查看>>
AfxMessageBox和MessageBox差别
查看>>
OpenCms创建站点过程图解——献给OpenCms的刚開始学习的人们
查看>>
PyQt5在QWidget窗体中显示Qwidget的自定义类(补:完美解决)
查看>>
一个非常好用的中文语音播报接口
查看>>
USB Host的上拉下拉电阻
查看>>
读书笔记系列之java性能优化权威指南 一 第一章
查看>>
结构体 typedef关键字
查看>>
Android设备连接Unity Profiler性能分析器
查看>>
PL/SQL中复制中文再粘贴出现乱码问题的解决【转】
查看>>
【分享】博客美化(8)让你的博客“推荐按钮”动起来
查看>>