zoukankan      html  css  js  c++  java
  • c#枚举 获取枚举键值对、描述等

    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, string> dic1 = GetEnumItemDesc(typeof(Days));
    
                foreach (string key in dic1.Keys)
                {
                    Console.WriteLine(key + ":{0}", dic1[key]);
                }
    
    
                Dictionary<string, string> dic = GetEnumItemValueDesc(typeof(Days));
                foreach (string key in dic.Keys)
                {
                    Console.WriteLine(key + ":{0}", dic[key]);
                }
    
                Console.WriteLine(string.Format(Days.Sunday.ToString() + ":{0}", GetEnumDesc(Days.Sunday)));
                Console.WriteLine(string.Format("{0}", (int)Enum.Parse(typeof(Days), "Thursday", true)));
                Console.ReadKey();
            }
    
            /// <summary>
            /// 获取枚举项描述信息 例如GetEnumDesc(Days.Sunday)
            /// </summary>
            /// <param name="en">枚举项 如Days.Sunday</param>
            /// <returns></returns>
            public static string GetEnumDesc(Enum en)
            {
                Type type = en.GetType();
                MemberInfo[] memInfo = type.GetMember(en.ToString());
                if (memInfo != null && memInfo.Length > 0)
                {
                    object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                    if (attrs != null && attrs.Length > 0)
                        return ((DescriptionAttribute)attrs[0]).Description;
                }
                return en.ToString();
            }
    
            ///<summary>
            /// 获取枚举项+描述
            ///</summary>
            ///<param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
            ///<returns>键值对</returns>
            public static Dictionary<string, string> GetEnumItemDesc(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="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
            ///<returns>键值对</returns>
            public static Dictionary<string, string> GetEnumItemValueDesc(Type enumType)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                Type typeDescription = typeof(DescriptionAttribute);
                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;
                        }
                        dic.Add(strValue, strText);
                    }
                }
                return dic;
            }
        }
    
        public enum Days
        {
            [Description("星期天")]
            Sunday,
            [Description("星期一")]
            Monday,
            [Description("星期二")]
            Tuesday,
            [Description("星期三")]
            Wednesday,
            [Description("星期四")]
            Thursday,
            [Description("星期五")]
            Friday,
            [Description("星期六")]
            Saturday
        }
    }
    



  • 相关阅读:
    centos7下安装docker(2镜像)
    Centos7下安装docker(1)
    分享一个连接,升级内核
    zabbix图形乱码问题解决办法
    六十四:JavaScript之JavaScript的Math对象常用的方法
    六十三:JavaScript之JavaScript的String对象常用的方法
    六十二:JavaScript之JavaScript数组对象和常用的方法
    六十一:JavaScript之JavaScript函数
    六十:JavaScript之JavaScript流程控制语句
    五十九:JavaScript之JavaScript操作符
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234063.html
Copyright © 2011-2022 走看看