C# 枚举相关

话不多说,还是直接看以下代码:主要由:

 public class CommonHandler
    {
        /// <summary>
        /// 根据枚举值来获取描述信息
        /// </summary>
        /// <param name="e">枚举值</param>
        /// <returns></returns>
        public static string GetEnumDesc(Enum e)
        {
            DescriptionAttribute[] descAttribute = (DescriptionAttribute[])e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
            return descAttribute == null || descAttribute.Length == 0 ? string.Empty : descAttribute[0].Description;
        }


        /// <summary>
        /// 根据枚举类型来获取枚举值和枚举描述信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<int, string> GetValueAndDesc<T>()
        {


            Dictionary<int, string> dic = new Dictionary<int, string>();
            try
            {
                foreach (FieldInfo item in typeof(T).GetFields())
                {
                    if (item.FieldType.IsEnum)
                    {
                        int key = (int)typeof(T).InvokeMember(item.Name, BindingFlags.GetField, null, null, null);
                        DescriptionAttribute[] descs = (DescriptionAttribute[])item.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (descs.Length > 0 && !dic.ContainsKey(key))
                        {
                            dic.Add(key, descs[0].Description);
                        }
                    }
                }
            }
            catch (Exception)
            {
               // throw;
            }
            return dic;

        }

      //泛型方法,返回的是枚举中的Description()中的描述值和枚举实际值(int)

        public static Dictionary<string, int> GetValueAndDesc_Key<T>()类型
        {


            Dictionary<string, int> dic = new Dictionary<string, int>();
            try
            {
                foreach (FieldInfo item in typeof(T).GetFields())
                {
                    if (item.FieldType.IsEnum)
                    {
                        int key = (int)typeof(T).InvokeMember(item.Name, BindingFlags.GetField, null, null, null);
                        DescriptionAttribute[] descs = (DescriptionAttribute[])item.GetCustomAttributes(typeof(DescriptionAttribute), false);
                        if (descs.Length > 0 && !dic.ContainsKey((string)descs[0].Description))
                        {
                            dic.Add( descs[0].Description,key);
                        }
                    }
                }
            }
            catch (Exception)
            {
                // throw;
            }
            return dic;
        }
        public static int GetEnumValueByDesc<T>(string desc)
        {
            if (string.IsNullOrEmpty(desc))
            {
                throw new Exception("传入的参数desc为空");
            }
            Dictionary<string, int> dicAll = GetValueAndDesc_Key<T>();
            if (dicAll!=null&&dicAll.Count>0)
            {
                foreach (string item in dicAll.Keys)
                {
                    if (string.Compare(item,desc,false)==0)
                    {
                        return dicAll[item];
                        break;
                    }
                }
            }
            return int.MaxValue;
        }

    }

}

以上的代码全部经过测试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值