首页 > 代码库 > 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() { } }
定义一个枚举,想要显示 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(转)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。