特性Atrribute和枚举
特性的简单实用!特性存放在metedata里面,它离不开反射。
Program.cs
class Program { static void Main(string[] args) { Console.WriteLine(PersonOptions.PersonOne.ToString() + " : " + PersonOptions.PersonOne.GetDescription()); Console.WriteLine(PersonOptions.PersonTwo.ToString() + " : " + PersonOptions.PersonTwo.GetDescription()); Console.WriteLine(PersonOptions.PersonThree.ToString() + " : " + PersonOptions.PersonThree.GetDescription()); Console.ReadLine(); } }
PersonOptions.cs
public enum PersonOptions { [Description("张三")] PersonOne, [Description("李四")] PersonTwo, [Description("王五")] PersonThree }
DescriptionAttribute.cs
public class DescriptionAttribute : Attribute { public string Description { get; set; } public DescriptionAttribute(string description) { this.Description = description; } }
EnumHelper.cs 扩展方法和反射的使用来获得Description.
public static class EnumHelper { public static string GetDescription(this Enum option) { string description = ""; FieldInfo fieldInfo = option.GetType().GetField(option.ToString()); var enumOption = (DescriptionAttribute)fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute)); if (enumOption != null) { description = enumOption.Description; } return description; } }
特性Atrribute和枚举的更多相关文章
- 【点滴积累】通过特性(Attribute)为枚举添加更多的信息
转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...
- [asp.net mvc 奇淫巧技] 03 - 枚举特性扩展解决枚举命名问题和支持HtmlHelper
一.需求 我们在开发中经常会遇到一些枚举,而且这些枚举类型可能会在表单中的下拉中,或者单选按钮中会用到等. 这样用是没问题的,但是用过的人都知道一个问题,就是枚举的命名问题,当然有很多人枚举直接中文命 ...
- c#枚举值增加特性说明
c#枚举值增加特性说明 通过特性给一个枚举类型每个值增加一个字符串说明,用于打印或显示. 自定义打印特性 [AttributeUsage(AttributeTargets.Field)] public ...
- 【C#进阶系列】18 特性Attribute
这个东西有的叫定制特性,然而我喜欢直接叫特性,但是这样的话一些人不知道我说的是什么,如果我说是Attribute的话那么知道的或者用过的就都懂了. 还记得讲到枚举和位标志那一章,关于位标志,有一个[F ...
- 你必须知道的.NET之特性和属性(转)
1. 引言 attribute是.NET框架引入的有一技术亮点,因此我们有必要花点时间走进一个发现attribute登堂入室的入口.因为.NET Framework中使用了大量的定制特性来完成代码约定 ...
- C#的惰性枚举
Ruby 2.0有一个新的特性是惰性枚举器,Soi Mort 的博客举了一个例子:可以将下面的代码 File.open(path) {|fp| fp.each_line. \ select {|lin ...
- JavaEE基础(二十七)/反射、JDK新特性
1.反射(类的加载概述和加载时机) A:类的加载概述 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 加载 就是指将class文件读入 ...
- iOS 深入Objective-C的动态特性
深入Objective-C的动态特性 Objective-C具有相当多的动态特性,基本的,也是经常被提到和用到的有动态类型(Dynamic typing),动态绑定(Dynamic binding)和 ...
- .NET之特性和属性
1. 引言 attribute是.NET框架引入的有一技术亮点,因此我们有必要花点时间走进一个发现attribute登堂入室的入口.因为.NET Framework中使用了大量的定制特性来完成代码约定 ...
随机推荐
- HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别
①HashMap的工作原理 HashMap基于hashing原理,我们通过put()和get()方法储存和获取对象.当我们将键值对传递给put()方法时,它调用键对象的hashCode()方法来计算h ...
- CAST()函数
语法: CAST(expression AS data_type) 参数说明: expression:任何有效的SQServer表达式 AS:用于分割两个参数,在AS之前的是需要处理的数据,在AS之后 ...
- python网络编程【四】(域名系统)
域名系统(DNS)是一个分布式的数据库,它主要用来把主机名转换成IP地址.DNS以及相关系统之所以存在,主要有以下两个原因: (1).它们可以使人们比较容易地记住名字. (2).它允许服务器改变IP地 ...
- Bootstrap <基础十八>面包屑导航(Breadcrumbs)
面包屑导航(Breadcrumbs)是一种基于网站层次信息的显示方式.以博客为例,面包屑导航可以显示发布日期.类别或标签.它们表示当前页面在导航层次结构内的位置. Bootstrap 中的面包屑导航( ...
- VBA续嘘嘘
什么是VBA?它有什么作用? A.实现Excel中没有实现的功能. B.提高运行速度. C.编写自定义函数. D.实现自动化功能. E.通过插入窗体做小型管理软件. VBA在哪里存放的?怎么运行? A ...
- wordpress 获取特色图片url方法
制作主题是需要获取特色图片,直接获取到url能更好的编辑css样式 <?php $large_image_url = wp_get_attachment_image_src( get_post_ ...
- Js 根据不同浏览器弹出窗口
/// <reference path="intellisense/jquery-1.2.6-vsdoc.js" /> var userAgent = navigato ...
- Spring Shedule Task之注解实现 (两次启动Schedule Task 的解决方案)
在spring 中的新引入的task 命名空间.可以部分取代 quartz 功能,配置和API更加简单,并且支持注解方式. 第一步: 在Spring的相关配置文件中(applicationContex ...
- c++作业:Circle
Circle Github链接
- Cannot open the disk 'D:\Program Files\VMOS\Centos.vmdk' or one of the snapshot disks it depends on
启动vmware时出现以下错误: Cannot open the disk 'D:\Program Files\VMOS\Centos.vmdk' or one of the snapshot dis ...