Enum To List(转)
Posted plina
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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() { } }
定义一个枚举,想要显示 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; } }
代码如下:
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; }
var query = EnumToList<ResourceState>().Where(e => e.Classification >= 1 && e.Classification <= 6).OrderBy(e => e.Classification).ToList();
以上是关于Enum To List(转)的主要内容,如果未能解决你的问题,请参考以下文章
Ruby 中 [1,2,3].to_enum 和 [1,2,3].enum_for 的区别
LeetCode-面试算法经典-Java实现114-Flatten Binary Tree to Linked List(二叉树转单链表)