using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Common
{
public static class EnumHelper
{
///
/// 私有,,获取彩种配置的方法
///
///
public static IEnumerable GetEnumList()
{
var list = new List();
foreach (var field in typeof(T).GetFields())
{
if (!field.IsSpecialName)
{
var d = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
var o = field.GetCustomAttributes(typeof(OtherAttribute), true);
list.Add(new EnumModel
{
Key = field.Name,
Value = (int)field.GetRawConstantValue(),
Description = d.Count() > 0 ? (d[0] as DescriptionAttribute).Description : "",
Other = o.Count() > 0 ? (o[0] as OtherAttribute).Name : "",
});
}
}
return list;
}
///
/// 通过key获取EnumModel
///
///
///
///
public static EnumModel GetEnumModelByKey(this List list, string value)
{
return list.Where(p => p.Key == value).FirstOrDefault();
}
public static EnumModel GetEnumModelByKey(this List list, Enum value)
{
return list.Where(p => p.Key == value.ToString()).FirstOrDefault();
}
///
/// 根据EnumModel的Enum获取相应的详情
///
///
///
///
public static string GetEnumDescription(this List list, Enum value)
{
return list.Where(p => p.Key == value.ToString()).FirstOrDefault().Description;
}
}
}