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";
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM