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. Sqlstate解释

    本篇文章主要介绍了"Sqlstate详解",主要涉及到方面的内容,对于DB2感兴趣的同学可以参考一下: 根据 X/Open 和 SQL Access Group SQL CAE 规 ...

  2. android下使用smack需引入的包

    compile 'org.igniterealtime.smack:smack-android:4.2.0-alpha1' compile 'org.igniterealtime.smack:smac ...

  3. jsp导出excel

    很多时候,我们都知道在java项目里面采用poi来导出excel很方便,但是如果你的项目采用的是jsp你可以用更简单的方法来导出.首先你要在顶部引入:<jsp:directive.page im ...

  4. Android HTTPS(5)SSL测试工具

    Nogotofail: A Network Traffic Security Testing Tool Nogotofail is a tool gives you an easy way to co ...

  5. Mtk Android 打包解包*.img

    打包/解包 boot.img, system.img, userdata.img, or recovery.img [DESCRIPTION] MTK codebase编译出来的image必须使用MT ...

  6. Python3 学习第九弹: 模块学习二之文件管理模块

    os模块 提供访问操作系统的接口 1> name 获得当前操作系统 其中 'nt' 是 windows 'posix' 是 linux 2> environ 获得当前系统的环境变量的字典, ...

  7. laravel, Composer和autoloading

    http://www.php-fig.org/psr/psr-4/ http://www.php-fig.org/psr/psr-0/ http://alanstorm.com/laravel_com ...

  8. splay入门

    在比较了网上的几份模板的速度之后,发现指针版明显快了很多,但是一敲起来....各种不习惯...所以还是学的hzwer 的数组版... bzoj3223:维护reverse操作就可以了 #include ...

  9. [转]FFMPEG视音频编解码零基础学习方法

    在CSDN上的这一段日子,接触到了很多同行业的人,尤其是使用FFMPEG进行视音频编解码的人,有的已经是有多年经验的“大神”,有的是刚开始学习的初学者.在和大家探讨的过程中,我忽然发现了一个问题:在“ ...

  10. 【量化】docker

    查看docker docker ps docker ps -a 删除docker docker stop 8809752ca95a docker rm 8809752ca95a 打包fly cd ~/ ...