System.ComponentModel命名空间下有个名为DescriptionAttribute的类用于指定属性或事件的说明,我所调用的枚举值描述信息就是DescriptionAttribute类的Description属性值。

首先定义一个枚举

/// <summary>
    /// 测试用的枚举
    /// </summary>
    public enum ArticleTypeList
    {
        [DescriptionAttribute("中国软件开发网络")]
        csdn,        
        msdn,
        [DescriptionAttribute("博客园")]
        cnblogs,
        other
    }

默认情况下我们采用ArticleTypeList.csdn.ToString()的方式只能得到“csdn”,而不是“中国软件开发网络”,为了获取“中国软件开发网络”,我定义了下面这样一个静态方法:

/// <summary>
        /// 获取枚举类子项描述信息
        /// </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>
    /// 自定义的一个属性类
    /// </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>
    /// 测试用的枚举
    /// </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>
        /// 获取枚举类子项描述信息
        /// </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相关值的调用如下所示:

GetselfAttributeInfo(ArticleTypeList.csdn, out text, out test);

相关源码和工程下载:http://files.cnblogs.com/cncxz/EnumDescription.rar

利用DescriptionAttribute定义枚举值的描述信息 z的更多相关文章

  1. 在C#中如何读取枚举值的描述属性

    在C#中,有时候我们需要读取枚举值的描述属性,也就是说这个枚举值代表了什么意思.比如本文中枚举值 Chinese ,我们希望知道它代表意思的说明(即“中文”). 有下面的枚举: 1 2 3 4 5 6 ...

  2. C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

    /// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...

  3. 利用DescriptionAttribute实现枚举字符串

    我们知道定义枚举时是不允许带空格等符号的,这样就不利于进行字符串对比.当然可以通过给枚举添加DescriptionAttribute,然后通过fieldinfo读取DescriptionAttribu ...

  4. 在C#中读取枚举值的描述属性

    枚举: public enum EnumLanugage { [System.ComponentModel.Description("中文")] Chinese, English ...

  5. C# 枚举类型的描述信息获取

    新建一个控制台方法,写好自己的枚举类型: 如图: 在里面添加获取描述的方法: 具体源码: 链接:http://pan.baidu.com/s/1nv4rGkp 密码:byz8

  6. 获取Enum枚举值描述的几法方法

    原文:获取Enum枚举值描述的几法方法 1.定义枚举时直接用中文 由于VS对中文支持的很不错,所以很多程序员都采用了此方案. 缺点:1.不适合多语言 2.感觉不太完美,毕竟大部分程序员大部分代码都使用 ...

  7. 【点滴积累】通过特性(Attribute)为枚举添加更多的信息

    转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...

  8. C#将一个枚举里面所有描述和value绑定到下拉列表的方法

    /// <summary> /// 获取枚举值的描述,如果没有描述,则返回枚举名称 /// </summary> /// <param name="en&quo ...

  9. .NET获取枚举DescriptionAttribute描述信息性能改进的多种方法

    一. DescriptionAttribute的普通使用方式 1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比较常见的就是枚举,通过获取枚举上定义的描述信息在UI上显 ...

随机推荐

  1. 两个C++对象是否相等,要程序员自己下定义,通常是覆盖==操作符

    我曾经好多年对Java的==和equals的区别和联系搞不清楚,后来搞清楚了,笔记在这里: http://www.cnblogs.com/findumars/p/3240761.htmlhttp:// ...

  2. 自由软件VS开源软件

    自由软件VS开源软件 “自由软件运动”是一项倡导软件这种知识产品应该免费共享的社会运动,它主要是从社会伦理学,道德的高度,强调我们每个人都有自由使用软件的权利.这种权利不应该被软件私有所破坏. 反对软 ...

  3. ubuntu安装postgresql与postgis

    版本信息 ubuntu    14.04.1LTS postgresql   9.3.5 postgis       2.1.2 今天尝试着安装了postgis 2.1.2,(较简便的包安装,不是源码 ...

  4. javascript library

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  5. opencv实现KNN手写数字的识别

    人工智能是当下很热门的话题,手写识别是一个典型的应用.为了进一步了解这个领域,我阅读了大量的论文,并借助opencv完成了对28x28的数字图片(预处理后的二值图像)的识别任务. 预处理一张图片: 首 ...

  6. linux软件的安装,更新与卸载

    Linux常见的安装为tar,zip,gz,rpm,deb,bin等.我们可以简单的分为三类. 第一:打包或压缩文件tar,zip,gz等,一般解压后即可,或者解压后运行sh文件: 第二:对应的有管理 ...

  7. 【转载】两军问题与Paxos算法 & 动画讲解Paxos算法

    http://harry.me/blog/2014/12/27/neat-algorithms-paxos/ 这篇文章里面有用JS写的Paxos过程,有助理解.但是没怎么仔细看,没时间. 这篇文章用两 ...

  8. Qt之QHeaderView自定义排序(QSortFilterProxyModel)

    简述 对以上节的排序,我们衍伸了两点: 把一个字符串前面的数据按照字符串比较,而后面的数据按照整形比较. 将整形显示为字符串,而排序依然正常呢. 为了分别描述,这里我们先解决问题1. 简述 效果 处理 ...

  9. 使用Aspose.Cells组件生成Excel文件

    生成带表头的Excel文件,格式如下显示. 当然更复杂的一些也可以通过 合并单元格的方法 public void Merge(int firstRow, int firstColumn, int to ...

  10. 51nod1379 索函数

    果断打表找规律.然后看得出来是2^k-1之后又不知道怎么求出k有什么卵用... http://blog.csdn.net/guhaiteng/article/details/52094210 %%%% ...