C# 枚举转列表

using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Common.Utils
{
    public class EnumUtil
    {
        public static IList<EnumEntity> EnumToList<T>()
        {
            IList<EnumEntity> list = new List<EnumEntity>();
            foreach (var item in Enum.GetValues(typeof(T)))
            {
                EnumEntity enumEntity = new EnumEntity();
                Object[] objArr = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute descAttr = objArr[0] as DescriptionAttribute;
                    enumEntity.Desction = descAttr.Description;
                }
                enumEntity.EnumValue = Convert.ToInt32(item);
                enumEntity.EnumName = item.ToString();
                list.Add(enumEntity);
            }
            return list;
        }
    }

    public class EnumEntity
    {
        /// <summary>
        /// 枚举的描述
        /// </summary>
        public string Desction { set; get; }

        /// <summary>
        /// 枚举名称
        /// </summary>
        public string EnumName { set; get; }

        /// <summary>
        /// 枚举对象的值
        /// </summary>
        public int EnumValue { set; get; }
    }

}

 

posted @ 2018-09-12 11:29  _York  阅读(113)  评论(0编辑  收藏