C#枚舉擴充方法,擷取枚舉值的描述值以及擷取一個枚舉類下面所有的元素

來源:互聯網
上載者:User

標籤: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#枚舉擴充方法,擷取枚舉值的描述值以及擷取一個枚舉類下面所有的元素

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.