度量快速开发平台-专业、快速的软件定制快开平台
标题: .NET枚举类型转为List类型 [打印本页]
作者: Adam丶鱼 时间: 2020-3-26 20:20
标题: .NET枚举类型转为List类型
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();
}
作者: 张兴康 时间: 2020-3-27 18:05
欢迎光临 度量快速开发平台-专业、快速的软件定制快开平台 (http://p.delit.cn/) |
Powered by Discuz! X3.2 |