度量快速开发平台-专业、快速的软件定制快开平台

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 部件 流程 SQL
查看: 1157|回复: 1
打印 上一主题 下一主题

[分享] .NET枚举类型转为List类型

[复制链接]

182

主题

2120

帖子

4842

积分

论坛元老

Rank: 8Rank: 8

积分
4842
跳转到指定楼层
楼主
发表于 2020-3-26 20:20:41 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
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();
        }

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

542

主题

5916

帖子

1万

积分

作者

Rank: 7Rank: 7Rank: 7

积分
13589
沙发
发表于 2020-3-27 18:05:31 | 只看该作者
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|重庆度量科技  本站关键词:快速开发平台

GMT+8, 2024-9-20 18:45 , Processed in 0.167248 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表