• C# 枚举帮助类EnumHelper(获取描述、名称和数值)


    帮助类定义

        public class EnumHelper
        {
            #region 静态方法
            public static Dictionary<string, string> GetEnumDescription<T>()
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
    
                FieldInfo[] fields = typeof(T).GetFields();
    
                foreach (FieldInfo field in fields)
                {
    
                    if (field.FieldType.IsEnum)
                    {
    
                        object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
    
                        string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;
    
                        dic.Add(field.Name, description);
    
                    }
                }
    
                return dic;
            }
    
            /// <summary>        
            /// 获取对应的枚举描述        
            /// </summary>        
            public static List<KeyValuePair<string, string>> GetEnumDescriptionList<T>()
            {
                List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
    
                FieldInfo[] fields = typeof(T).GetFields();
    
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType.IsEnum)
                    {
    
                        object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
    
                        string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;
    
                        result.Add(new KeyValuePair<string, string>(field.Name, description));
    
                    }
    
                }
                return result;
            }
    
    
            /// <summary>
            /// 获取枚举的 值和描述
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public static List<KeyValuePair<int, string>> GetEnumValueDescriptionList<T>()
            {
                List<KeyValuePair<int, string>> result = new List<KeyValuePair<int, string>>();
                FieldInfo[] fields = typeof(T).GetFields();
                foreach (FieldInfo field in fields)
                {
                    if (field.FieldType.IsEnum)
                    {
                        object[] attr = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        string description = attr.Length == 0 ? field.Name : ((DescriptionAttribute)attr[0]).Description;
                        result.Add(new KeyValuePair<int, string>(Convert.ToInt32(field.GetValue(null)), description));
                    }
                }
    
                return result;
            }
    
            public static string GetDescriptionByEnumName<T>(string name)
            {
                try
                {
                    Dictionary<string, string> dic = GetEnumDescription<T>();
                    string description = dic[name];
                    return description;
                }
                catch (Exception ex)
                {
                    return "";
                }
            }
            #endregion
        }
    

    控件绑定

        public enum GenderType
        {
            [Description("男")]
            Man = 1,
            [Description("女")]
            Woman = 2
        }
    
        private void frmMain_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = EnumHelper.GetEnumValueDescriptionList<GenderType>().Select(x => new
            {
                Key = x.Value,
                Value = x.Key
            }).ToList();
            comboBox1.DisplayMember = "Key";
            comboBox1.ValueMember = "Value";
        }
    
  • 相关阅读:
    linux性能指令分析进阶篇
    数据库提升篇
    linux之基础命令大全
    数据库事务测试以及级联更新级联删除
    【Comet OJ】—模拟赛测试 Day1题解
    【Comet OJ】—模拟赛测试 Day1题解
    【LOJ # 6268】—分拆数(生成函数+多项式Ln/Exp+NTT)
    【LOJ # 6268】—分拆数(生成函数+多项式Ln/Exp+NTT)
    【LOJ #6041】【雅礼集训 2017 Day7】—事情的相似度(后缀自动机+LCT+树状数组)
    【LOJ #6041】【雅礼集训 2017 Day7】—事情的相似度(后缀自动机+LCT+树状数组)
  • 原文地址:https://www.cnblogs.com/zhaoshujie/p/14806402.html
Copyright © 2020-2023  润新知