Exemplo n.º 1
0
        /// <summary>
        /// 将枚举转成List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List <EnumberEntity> EnumToList <T>()
        {
            List <EnumberEntity> list = new List <EnumberEntity>();

            foreach (var e in Enum.GetValues(typeof(T)))
            {
                EnumberEntity m      = new EnumberEntity();
                object[]      objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    m.Des = da.Description;
                }
                m.Value = Convert.ToInt32(e);
                m.Title = e.ToString();
                list.Add(m);
            }
            return(list);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 根据枚举key来获取单个枚举实体的文字描述内容
 /// </summary>
 /// <typeparam name="T">枚举</typeparam>
 /// <param name="key">value</param>
 /// <returns></returns>
 public static string GetEnumDescriptionByKey <T>(string key)
 {
     foreach (var e in Enum.GetValues(typeof(T)))
     {
         EnumberEntity m      = new EnumberEntity();
         object[]      objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
         if (objArr != null && objArr.Length > 0)
         {
             DescriptionAttribute da = objArr[0] as DescriptionAttribute;
             m.Des = da.Description;
         }
         m.Value = Convert.ToInt32(e);
         m.Title = e.ToString();
         if (key == m.Title)
         {
             return(m.Des);
         }
     }
     return("");
 }