enum Orientation
{
[DescriptionAttribute("东")]
East,
[DescriptionAttribute("南")]
South,
[DescriptionAttribute("西")]
West,
[DescriptionAttribute("北")]
North
}

但又如何获取DescriptionAttribute的内容呢?呵呵,答案就是通过反射。下面是我封装获取DescriptionAttribute内容的扩展方法。

static string GetDescription<T>(this T value)
{
var memInfo = value.GetType().GetMember(value.ToString());
var attributes = memInfo[].GetCustomAttributes(typeof(DescriptionAttribute), false).Cast<DescriptionAttribute>();
if (attributes.Any())
return attributes.First().Description;
return string.Empty;
}

下面就是测试的代码以及测试结果

static class Program
{
enum Orientation
{
[DescriptionAttribute("东")]
East,
[DescriptionAttribute("南")]
South,
[DescriptionAttribute("西")]
West,
[DescriptionAttribute("北")]
North
}
static string GetDescription<T>(this T value)
{
var memInfo = value.GetType().GetMember(value.ToString());
var attributes = memInfo[].GetCustomAttributes(typeof(DescriptionAttribute), false).Cast<DescriptionAttribute>();
if (attributes.Any())
return attributes.First().Description;
return string.Empty;
}
static void Main()
{
foreach (var s in Enum.GetValues(typeof(Orientation)))
{
Console.WriteLine("值:{0}\t\t说明:{1}",s.ToString(),s.GetDescription());
}
}
}

从输出可以看出,程序成功的获取了枚举值的说明内容,这样就可以利用这种模式方便的对枚举值添加中文说明,然后在需要的时候提取出来,如:需要做数据绑定的时候。

为枚举类型添加说明 zt的更多相关文章

  1. [小技巧]C#中如何为枚举类型添加描述方法

    背景 在我们的日常开发中,我们会经常使用枚举类型.有时我们只需要显示枚举的值或者枚举值对应名称, 但是在某些场景下,我们可能需要将枚举值显示为不同的字符串. 例: 当前我们有如下枚举Level pub ...

  2. [CLR via C#]15. 枚举类型和位标志

    一.枚举类型 枚举类型(enumerated types)定义了一组"符号名称/值"配对. 例如,以下Color类型定义了一组符号,每个符号都标识一种颜色: internal en ...

  3. Effective Java 第三版——34. 使用枚举类型替代整型常量

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  4. 测者的测试技术手册:Junit单元测试遇见的一个枚举类型的坑(枚举类型详解)

    Enum的简介 枚举类型很早就在计算机语言中存在了,主要被用来将一组相似的值包含进一种类型中,这种类型的名称被定义成独一无二的类型描述符,这就是枚举类型. 在java语言中,枚举类型是一个完整功能的类 ...

  5. 【CLR Via C#】15 枚举类型与位类型

    1.基础 枚举类型(enumerated types)定义了一组“符号名称/值”配对. 枚举类型是值类型,每个枚举类型都是从System.Enum派生的,而System.Enum又是从System.V ...

  6. java基础(十一) 枚举类型

    枚举类型Enum的简介 1.什么是枚举类型 枚举类型: 就是由一组具有名的值的有限集合组成新的类型.(即新的类). 好像还是不懂,别急,咱们先来看一下 为什么要引入枚举类型 在没有引入枚举类型前,当我 ...

  7. <NET CLR via c# 第4版>笔记 第15章 枚举类型和位标志

    15.1 枚举类型 枚举定义的符号是常量值. C#编译器编译时,会用数值替换符号,不再引用定义了符号的枚举类型.可能会出现一些版本问题. Enum.IsDefined(Type enumType, o ...

  8. Java的枚举类型

    引用并转载于:http://blog.csdn.net/ishallwin/article/details/9440251 1.什么是枚举: 在实际编程中,往往存在着这样的“数据集”,它们的数值在程序 ...

  9. 重温CLR(十一) 枚举类型、位标志和数组

    枚举类型 枚举类型(enumerated types)定义了一组"符号名称/值"配对.例如,以下Color类型定义了一组符号,每个符号都标识一种颜色: internal enum ...

随机推荐

  1. maven3.1安装及配置

    1.首先下载maven3,并安装 2.环境变量配置与JAVA_HOME的配置一样 3.打开MyEclipse preferences>>Maven4MyEclipse>>Mav ...

  2. drupal CMS

    http://drupalchina.cn/ https://www.drupal.org

  3. 1201: [HNOI2005]数三角形 - BZOJ

    Description Input 大三角形的所有短边可以看成由(n+1)*n/2个单位三角形的边界组成.如下图的灰色三角形所示.其中第1排有1个灰色三角形,第2排有2个灰色三角形,……,第n排有n个 ...

  4. c++ 私有函数 头文件设计

    clock.h #ifndef CLOCK_H_INCLUDED #define CLOCK_H_INCLUDED class Clock {public: static void HandleExd ...

  5. idea14使用maven创建web工程

    参考博客:http://geeksun.iteye.com/blog/2179658 http://www.bubuko.com/infodetail-387387.html ------------ ...

  6. hdu 4192

    dfs全排列  加  模拟计算 #include <iostream> #include <cstdio> #include <cstdlib> #include ...

  7. python读写配置文件

    #coding:utf-8 import ConfigParser class Conf(): def __init__(self,name): self.name = name self.cp = ...

  8. 学习笔记:shared_ptr陷阱

    条款1:不要把一个原生指针给多个shared_ptr管理 int* ptr = new int; shared_ptr<int> p1(ptr); shared_ptr<int> ...

  9. 1962-Fibonacci

    描述 This is an easy problem.I think Fibonacci sequence is familiar to you.Now there is another one. H ...

  10. Tornado,表单处理,一样在行

    哟,处理流程还算自然... import os.path import random import tornado.httpserver import tornado.ioloop import to ...