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. 研究CPU的好文章以及博客

    留个爪,有空仔细看: http://blog.csdn.net/zhangxinrun/article/details/6918862 http://blog.csdn.net/gaijf/artic ...

  2. Mysql笔记——DDL

    数据库模式定义语言DDL(Data Definition Language),是用于描述数据库中要存储的现实世界实体的语言.一个数据库模式包含该数据库中所有实体的描述定义.   =========== ...

  3. SQL Server下实现利用SQL Server Agent Job对索引重建实现Balance Load

    昨天工作中遇到这样一个场景,有个项目需要把某台服务器下所有的表和索引都启用数据压缩(data_compression=page),已经启用了的表和索引就不需要再压缩一次了.统计一下后发现要运行的REB ...

  4. Myeclipse 自定义代码自动格式化(ctrl+alt+F)

    打开如图界面:preference->java->code style->formatter下的edit... 如设设置java代码多长换行:

  5. java读取某个文件夹下的所有文件

    import java.io.FileNotFoundException;import java.io.IOException;import java.io.File; public class Re ...

  6. python 调用 C++ code

    本文以实例code讲解python 调用 C++的方法. 1. 如果没有参数传递从python传递至C++,python调用C++的最简单方法是将函数声明为C可用函数,然后作为C code被pytho ...

  7. wiremock之录制和回放

    Recording is done by starting the standalone runner like this: $ java -jar wiremock-1.50-standalone. ...

  8. BZOJ 1452 Count(二维树状数组)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1452 题意:给出一个数字矩阵(矩阵中任何时候的数字均为[1,100]),两种操作:(1) ...

  9. BZOJ 1415 聪聪和可可(概率DP)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1415 题意:一个无向图,一个猫.一只老鼠.在任意时刻猫知道老鼠在哪个顶点上.每次移动猫先 ...

  10. [HIHO1328]逃离迷宫(bfs,位压)

    题目链接:http://hihocoder.com/problemset/problem/1328 这个题bfs到时候不止要存当前的坐标,还要存当前有哪几把钥匙.因为5把钥匙,所以可以直接用位来存,这 ...