利用DescriptionAttribute定义枚举值的描述信息 z
System.ComponentModel命名空间下有个名为DescriptionAttribute的类用于指定属性或事件的说明,我所调用的枚举值描述信息就是DescriptionAttribute类的Description属性值。
首先定义一个枚举
/// 测试用的枚举
/// </summary>
public enum ArticleTypeList
{
[DescriptionAttribute("中国软件开发网络")]
csdn,
msdn,
[DescriptionAttribute("博客园")]
cnblogs,
other
}默认情况下我们采用ArticleTypeList.csdn.ToString()的方式只能得到“csdn”,而不是“中国软件开发网络”,为了获取“中国软件开发网络”,我定义了下面这样一个静态方法:
/// 获取枚举类子项描述信息
/// </summary>
/// <param name="enumSubitem">枚举类子项</param>
public static string GetEnumDescription(Enum enumSubitem)
{
string strValue = enumSubitem.ToString();
FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);
Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (objs == null || objs.Length == 0)
{
return strValue;
}
else
{
DescriptionAttribute da = (DescriptionAttribute)objs[0];
return da.Description;
}
}直接通过GetEnumDescription(ArticleTypeList.csdn)便可获取到“中国软件开发网络”了,对于那些没有定义
DescriptionAttribute的子项则直接返回枚举值,例如
GetEnumDescription(ArticleTypeList.msdn)将返回“msdn”。
思路扩展
虽然使用DescriptionAttribute类基本上也满足了一般需求,可也不排除个别情况下需要多个描述值,针对这种情况,我们可以自System.Attribute类继承编写自定义属性类,例如:
/// 自定义的一个属性类
/// </summary>
public class selfAttribute : Attribute
{
public selfAttribute(string displayText, string displayTest)
{
m_DisplayText = displayText;
m_DisplayTest = displayTest;
}
private string m_DisplayText = string.Empty;
private string m_DisplayTest = string.Empty;
public string DisplayText
{
get { return m_DisplayText; }
}
public string DisplayTest
{
get { return m_DisplayTest; }
}
}然后调整一下ArticleTypeList的代码:
/// 测试用的枚举
/// </summary>
public enum ArticleTypeList
{
[DescriptionAttribute("中国软件开发网络"), selfAttribute("自定义:中国软件开发网络", "http://www.csdn.net")]
csdn,
[selfAttribute("自定义:MSDN2 Library", "http://msdn2.microsoft.com/zh-cn/library/default.aspx")]
msdn,
[DescriptionAttribute("博客园")]
cnblogs,
other
}最后编写调用的静态方法:
/// 获取枚举类子项描述信息
/// </summary>
/// <param name="enumSubitem">枚举类子项</param>
public static string GetEnumDescription(Enum enumSubitem)
{
Object obj = GetAttributeClass(enumSubitem, typeof(DescriptionAttribute));
if (obj == null)
{
return enumSubitem.ToString();
}
else
{
DescriptionAttribute da = (DescriptionAttribute)obj;
return da.Description;
}
}
public static void GetselfAttributeInfo(Enum enumSubitem,out string text,out string test)
{
Object obj = GetAttributeClass(enumSubitem, typeof(selfAttribute));
if (obj == null)
{
text=test= enumSubitem.ToString();
}
else
{
selfAttribute da = (selfAttribute)obj;
text= da.DisplayText;
test = da.DisplayTest;
}
}
/// <summary>
/// 获取指定属性类的实例
/// </summary>
/// <param name="enumSubitem">枚举类子项</param>
/// <param name="attributeType">DescriptionAttribute属性类或其自定义属性类 类型,例如:typeof(DescriptionAttribute)</param>
private static Object GetAttributeClass(Enum enumSubitem, Type attributeType)
{
FieldInfo fieldinfo = enumSubitem.GetType().GetField(enumSubitem.ToString());
Object[] objs = fieldinfo.GetCustomAttributes(attributeType, false);
if (objs == null || objs.Length == 0)
{
return null;
}
return objs[0];
}
这样一来,对于DescriptionAttribute类描述信息,调用方法不变,而selfAttribute相关值的调用如下所示:

相关源码和工程下载:http://files.cnblogs.com/cncxz/EnumDescription.rar
利用DescriptionAttribute定义枚举值的描述信息 z的更多相关文章
- 在C#中如何读取枚举值的描述属性
在C#中,有时候我们需要读取枚举值的描述属性,也就是说这个枚举值代表了什么意思.比如本文中枚举值 Chinese ,我们希望知道它代表意思的说明(即“中文”). 有下面的枚举: 1 2 3 4 5 6 ...
- C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素
/// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...
- 利用DescriptionAttribute实现枚举字符串
我们知道定义枚举时是不允许带空格等符号的,这样就不利于进行字符串对比.当然可以通过给枚举添加DescriptionAttribute,然后通过fieldinfo读取DescriptionAttribu ...
- 在C#中读取枚举值的描述属性
枚举: public enum EnumLanugage { [System.ComponentModel.Description("中文")] Chinese, English ...
- C# 枚举类型的描述信息获取
新建一个控制台方法,写好自己的枚举类型: 如图: 在里面添加获取描述的方法: 具体源码: 链接:http://pan.baidu.com/s/1nv4rGkp 密码:byz8
- 获取Enum枚举值描述的几法方法
原文:获取Enum枚举值描述的几法方法 1.定义枚举时直接用中文 由于VS对中文支持的很不错,所以很多程序员都采用了此方案. 缺点:1.不适合多语言 2.感觉不太完美,毕竟大部分程序员大部分代码都使用 ...
- 【点滴积累】通过特性(Attribute)为枚举添加更多的信息
转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...
- C#将一个枚举里面所有描述和value绑定到下拉列表的方法
/// <summary> /// 获取枚举值的描述,如果没有描述,则返回枚举名称 /// </summary> /// <param name="en&quo ...
- .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法
一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...
随机推荐
- 纯CSS实现的右侧底部简洁悬浮效果
我们见过很多页面右侧浮动效果,最早有QQ联系面板,对联广告等,大多数都是基于Javascript实现的动态效果,今天我给大家分享一个只需要CSS结合DIV实现的右侧浮动效果. HTML 我们希望悬浮效 ...
- C 中数组和指针的区别
联系: 1,一个通过数组和下标实现的表达式可等价地通过指针和偏移量实现. 2,当数组名传递给一个函数时,实际上传递的是该数组第一个元素的地址. 区别: 1,指针是一个变量,因此,在C语言中,语句pa= ...
- Combiner
如果job 设置了 combiner ,则job的每个map运行的数据会先进入combiner,然后再通过patitioner分发到reduce.通过combiner能减少reduce的计算.空间压力 ...
- 使用Windows Azure PowerShell远程管理Windows Azure虚拟机
对于Windows Azure,如果你还在使用windowsazure.com门户来管理虚拟机,那就显得不怎么高上大了.Windows Azure PowerShell 是一个功能强大的脚本环境,可用 ...
- asp.net MVC3 “System.Web.Mvc.ModelClientValidationRule”问题
错误提示: Error 1 The type 'System.Web.Mvc.ModelClientValidationRule' exists in both 'c:\Program Files ( ...
- Hopcroft-Karp模板学习小结
最开始是因为做了一个题目接触到这个算法的,但是对于这个算法很多资料都只说了大概的方法: 首先从所有X的未盖点进行BFS,BFS之后对每个X节点和Y节点维护距离标号,如果Y节点是未盖点那么就找到了一条最 ...
- Android--数据持久化之内部存储、Sdcard存储
前言 之前一直在讲AndroidUI的内容,但是还没有完结,之后会慢慢补充.今天讲讲其他的,关于数据持久化的内容.对于一个应用程序而言,不可避免的要能够对数据进行存储,Android程序也不例外.而在 ...
- Maven下使用Jetty进行Debug
1.环境和条件 Maven-3.0.3Eclipse请阅读者事先具备一定maven知识 2 配置2.1 添加Jetty插件 在pom.xml中加入如下代码段 <plugin> <gr ...
- hihoCoder 1082然而沼跃鱼早就看穿了一切 (字符串处理)
http://hihocoder.com/problemset/problem/1082 首先将字符串全部字母变成小写,不断用find查找字符串中的Marshtomp,并把每个字符变为’#‘ ,最后统 ...
- URAL1495. One-two, One-two 2(dp)
1495 牵扯一点数位 保存数的时候我是按2进制保存的 把1当作0算 把2当作1算 滚动数组 dp[i][j][(g*10+j)%n] = min(dp[i][j][(g*10+j)%n],dp[i- ...