首页 > 代码库 > Enum To List(转)

Enum To List(转)

技术分享
    public enum ResourceState
    {
        /// <summary>
        /// 下架
        /// </summary>
        [Description("下架")]
        SoldOut = 0,
        /// <summary>
        /// 上架
        /// </summary> 
        [Description("上架")]
        Putaway = 1,

        /// <summary>
        /// 交易成功
        /// </summary>
        [Description("交易成功")]
        Success = 2,

        /// <summary>
        /// 废标
        /// </summary>
        [Description("废标")]
        AbandonedTender = 6,
        /// <summary>
        /// 违约标
        /// </summary>
        [Description("违约标")]
        DefaultMark = 7,

        /// <summary>
        /// 中标
        /// </summary>
        [SetClassification(Type = 5)]
        [Description("中标")]
        WinTheBidding = 3,
        /// <summary>
        /// 流标
        /// </summary>
        [SetClassification(Type = 6)]
        [Description("流标")]
        FlowStandard = 4,
        /// <summary>
        /// 未中标
        /// </summary>
        [SetClassification(Type = 4)]
        [Description("未中标")]
        LoseABid = 5,
        /// <summary>
        /// 竞价中
        /// </summary>
        [SetClassification(Type = 2)]
        [Description("竞价中")]
        Bidding = 8,

        /// <summary>
        /// 竞拍中
        /// </summary>
        [SetClassification(Type = 3)]
        [Description("竞拍中")]
        Auctioning = 9,

        /// <summary>
        /// 已处理(针对于流标资源)
        /// </summary>
        [Description("已处理")]
        Alreadyprocessed = 10,

        /// <summary>
        /// 已过期
        /// </summary>
        [Description("已过期")]
        ExpiredTime = 11,

        /// <summary>
        /// 所有报价
        /// </summary>
        [SetClassification(Type = 1)]
        [Description("所有报价")]
        All = 12


    }

    /// <summary>
    /// 添加自定义属性 

    /// 作用:过滤枚举类型
    /// </summary>
    public class SetClassificationAttribute : Attribute
    {
        /// <summary>
        /// 分类
        /// </summary>
        public int Type { get; set; }

        public SetClassificationAttribute() { }

    }
View Code

定义一个枚举,想要显示 classification type 从1 到 6的数据到一个list,list中对象有描述,值,type等属性

技术分享
public class EnumberCreditType
    {
        /// <summary>  
        /// 枚举的描述  
        /// </summary>  
        public string Desction { set; get; }

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

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

        /// <summary>
        /// 描述
        /// </summary>
        public string Name { get; set; }


        /// <summary>
        /// 分类
        /// </summary>
        public int Classification { set; get; }
    }
View Code

代码如下:

技术分享
   public static List<EnumberCreditType> EnumToList<T>()
        {
            List<EnumberCreditType> list = new List<EnumberCreditType>();

            foreach (var e in Enum.GetValues(typeof(T)))
            {
                EnumberCreditType m = new EnumberCreditType();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    m.Desction = da.Description;
                }
                //SetClassification 
                object[] setClassificationArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(SetClassificationAttribute), true);
                if (setClassificationArr != null && setClassificationArr.Length > 0)
                {
                    SetClassificationAttribute da = setClassificationArr[0] as SetClassificationAttribute;
                    m.Classification = da.Type;
                }
                //Display
                object[] disArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DisplayNameAttribute), true);
                if (disArr != null && disArr.Length > 0)
                {
                    DisplayNameAttribute da = disArr[0] as DisplayNameAttribute;
                    m.Name = da.DisplayName;
                }

                m.Value = Convert.ToInt32(e);
                m.Key = e.ToString();
                list.Add(m);
            }
            return list;
        }
View Code

var query = EnumToList<ResourceState>().Where(e => e.Classification >= 1 && e.Classification <= 6).OrderBy(e => e.Classification).ToList();

Enum To List(转)