码迷,mamicode.com
首页 > Windows程序 > 详细

C#枚举显示汉字

时间:2020-03-23 16:51:55      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:==   lse   null   pac   它的   nat   com   menu   map   

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
    {
        上午,
        下午,
        晚上
    };
}

C#枚举显示汉字

标签:==   lse   null   pac   它的   nat   com   menu   map   

原文地址:https://www.cnblogs.com/xl4ng/p/12552851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!