zoukankan      html  css  js  c++  java
  • 利用DescriptionAttribute定义枚举值的描述信息

    http://www.cnblogs.com/chengxiaohui/articles/1980088.html

    我们知道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相关值的调用如下所示:


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

  • 相关阅读:
    解析ASP.NET Mvc开发之删除修改数据
    JavaScript module pattern精髓
    Remote验证及其改进(附源码)
    图模型的精确推理
    C#在泛型类中,通过表达式树构造lambda表达式
    类管理指针成员
    朴素贝页斯分类法
    使用Repository模式构建数据库访问层
    利用MVC的过滤器实现url的参数加密和解密
    在多线程中使用静态方法是否有线程安全问题
  • 原文地址:https://www.cnblogs.com/carl2380/p/2280532.html
Copyright © 2011-2022 走看看