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

C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

时间:2017-08-01 19:05:48      阅读:500      评论:0      收藏:0      [点我收藏+]

标签:code   convert   遍历   add   string   对象   tostring   empty   tom   

/// <summary>
    /// 枚举扩展方法
    /// </summary>
    public static class EnumExtension
    {
        private static Dictionary<string, Dictionary<string, string>> _enumCache;

        /// <summary>
        /// 缓存
        /// </summary>
        private static Dictionary<string, Dictionary<string, string>> EnumCache
        {
            get { return _enumCache ?? (_enumCache = new Dictionary<string, Dictionary<string, string>>()); }
            set { _enumCache = value; }
        }

        /// <summary>
        /// 获取枚举描述信息
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static string GetEnumText(this System.Enum en)
        {
            string enString = string.Empty;
            if (null == en) return enString;

            Type type = en.GetType();
            enString = en.ToString();
            if (!EnumCache.ContainsKey(type.FullName))
            {
                System.Reflection.FieldInfo[] fields = type.GetFields();
                Dictionary<string, string> temp = new Dictionary<string, string>();
                foreach (FieldInfo item in fields)
                {
                    object[] attrs = item.GetCustomAttributes(typeof(TextAttribute), false);
                    if (attrs.Length == 1)
                    {
                        string v = ((TextAttribute)attrs[0]).Value;
                        temp.Add(item.Name, v);
                    }
                }
                EnumCache.Add(type.FullName, temp);
            }
            if (EnumCache[type.FullName].ContainsKey(enString))
            {
                return EnumCache[type.FullName][enString];
            }
            return enString;
        }

        /// <summary>
        /// 遍历枚举对象的所有元素
        /// </summary>
        /// <typeparam name="T">枚举对象</typeparam>
        /// <returns>Dictionary:枚举值-描述</returns>
        public static Dictionary<int, string> GetEnumValues<T>()
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            foreach (var code in System.Enum.GetValues(typeof(T)))
            {
                ////获取名称
                //string strName = System.Enum.GetName(typeof(T), code);

                object[] objAttrs = code.GetType().GetField(code.ToString()).GetCustomAttributes(typeof(TextAttribute), true);
                if (objAttrs.Length > 0)
                {
                    TextAttribute descAttr = objAttrs[0] as TextAttribute;
                    if (!dictionary.ContainsKey((int)code))
                    {
                        if (descAttr != null) dictionary.Add((int)code, descAttr.Value);
                    }
                    //Console.WriteLine(string.Format("[{0}]", descAttr.Value));
                }
                //Console.WriteLine(string.Format("{0}={1}", code.ToString(), Convert.ToInt32(code)));
            }
            return dictionary;
        }

    }

    /// <summary>
    /// 自定义描述
    /// </summary>
    public class TextAttribute : Attribute
    {
        public TextAttribute(string value)
        {
            Value = value;
        }

        /// <summary>
        /// 描述信息
        /// </summary>
        public string Value { get; set; }
    }

 

C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

标签:code   convert   遍历   add   string   对象   tostring   empty   tom   

原文地址:http://www.cnblogs.com/zhao-yi/p/7269734.html

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