zoukankan      html  css  js  c++  java
  • C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

    /// <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; }
        }
  • 相关阅读:
    Android Studio 配置Gradle总结
    ion-icon
    Centos 7下利用crontab定时执行任务详解
    Centos7 下安装以及使用mssql
    Docker容器
    linux centos7--linux和window共享文件(samba)
    一些CMS网站系统漏洞,练手用(持续更新)
    相应的游戏服务器组件信息不存在,房间创建失败
    [Windows] php开发工具,zendstudio13使用方法补丁
    mysql sql语句大全
  • 原文地址:https://www.cnblogs.com/zhao-yi/p/7269734.html
Copyright © 2011-2022 走看看