zoukankan      html  css  js  c++  java
  • 反射获取枚举特性的小例子

    代码如下:

    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Reflection;
    using System.ComponentModel;
    
    
    namespace EnumApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                NameValueCollection nvc = GetNVCFromEnumValue(typeof(TimeOfDay));
    
                foreach (string key in nvc.Keys)
                {
                    Console.WriteLine(string.Format(key + ": {0}", nvc[key]));
                }
                
                Dictionary<string, string> dic = GetEnumDic(typeof(TimeOfDay));
                foreach(string key in dic.Keys)
                {
                    Console.WriteLine(key+":{0}",dic[key]);
                }
    
                Console.WriteLine(string.Format(TimeOfDay.Moning.ToString() + ":{0}", GetEnumDes(TimeOfDay.Moning)));           
            }
    
            /// <summary>
            /// 从枚举类型和它的特性读出并返回一个键值对
            /// </summary>
            /// <param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
            /// <returns>键值对</returns>
            public static NameValueCollection GetNVCFromEnumValue(Type enumType)
            {
                NameValueCollection nvc = new NameValueCollection();
                Type typeDescription = typeof(DescriptionAttribute);
                System.Reflection.FieldInfo[] fields = enumType.GetFields();
                string strText = string.Empty;
                string strValue = string.Empty;
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType.IsEnum)
                    {
                        strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                        object[] arr = field.GetCustomAttributes(typeDescription, true);
                        if (arr.Length > 0)
                        {
                            DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                            strText = aa.Description;
                        }
                        else
                        {
                            strText = field.Name;
                        }
                        nvc.Add(strValue,strText);
                    }
                }
                return nvc;
            }
            /// <summary>
            /// 返回 Dic<枚举项,描述>
            /// </summary>
            /// <param name="enumType"></param>
            /// <returns>Dic<枚举项,描述></returns>
            public static Dictionary<string, string> GetEnumDic(Type enumType)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                FieldInfo[] fieldinfos = enumType.GetFields();
                foreach (FieldInfo field in fieldinfos)
                {
                    if (field.FieldType.IsEnum)
                    {
                        Object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
    
                        dic.Add(field.Name, ((DescriptionAttribute)objs[0]).Description);
                    }
    
                }
    
                return dic;
            }
            /// <summary>
            /// 获得某个枚举项的描述
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public static string GetEnumDes(object value)
            {
                FieldInfo fieldinfo = value.GetType().GetField(value.ToString());
                Object[] objs = fieldinfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (objs == null || objs.Length == 0)
                {
                    return value.ToString();
                }
                else
                {
                    return ((DescriptionAttribute)objs[0]).Description;                
                }
            }
        }
    
        public enum TimeOfDay
        {
            [Description("上午")]
            Moning=0,
            [Description("下午")]
            Afternoon,
            [Description("晚上")]
            Evening,
        };
    
        public enum TimeOfDays
        {
            上午,
            下午,
            晚上
        };
    }
  • 相关阅读:
    HSV 武胜
    crystalReportViewer+ReceiveCrystalReport.rpt+DataSet.xsd做报表 转 武胜
    Apache Gzip设置
    Linux gsoap 访问Jira
    搜索引擎Senna
    c 条件编译 ifndef解决 gcc multiple define error.
    搜索引擎 apachesolr
    Lucene:基于Java的全文检索引擎简介
    搜索引擎Zend_lucene
    基于应用程序级的Apache认证配置(普通认证篇)
  • 原文地址:https://www.cnblogs.com/jangwewe/p/3233110.html
Copyright © 2011-2022 走看看