.NET枚举类型转为List类型

2018-06-22 07:47:17来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

如图所示这个竞卖状态,原先是在前端界面通过html代码写死的几个状态,现在需要改为动态加载。这个几个状态是定义的枚举类型。

 

1:定义一个枚举类型

    /// <summary>
    /// 资源状态
    /// </summary>
    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


    }

2:自定义一个标记类型继承Attribute

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

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

        public SetClassificationAttribute() { }

    }

3:自定义返回List类型

    /// <summary>
    /// 自定义返回值类型
    /// </summary>
    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; }
    }

4:枚举转为List<EnumberCreditType>方法

        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        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(DisplayAttributes), true);
                if (disArr != null && disArr.Length > 0)
                {
                    DisplayAttribute da = disArr[0] as DisplayAttribute;
                    m.Name = da.Name;
                }

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

5:使用

        static void Main(string[] args)
        {

            // 获取枚举类型转为List,使用List的Where过滤条件
            var query =  _enumAppservice.EnumToList<ResourceState>().Where(e => e.Classification>=1&&e.Classification<=6).OrderBy(e=>e.Classification).ToList();
            Console.ReadKey();
        }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:windows下使用IIS的ARR实现站点的负载均衡

下一篇:一、Web Service简介