using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace NCC.Code
{
///
/// 枚举帮助类
///
public class EnumHelper
{
///
/// 返回枚举值的描述信息。
///
/// 要获取描述信息的枚举值。
/// 枚举值的描述信息。
public static string GetEnumDesc(int value)
{
try
{
Type enumType = typeof(T);
DescriptionAttribute attr = null;
// 获取枚举常数名称。
string name = Enum.GetName(enumType, value);
if (name != null)
{
// 获取枚举字段。
FieldInfo fieldInfo = enumType.GetField(name);
if (fieldInfo != null)
{
// 获取描述的属性。
attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
}
}
// 返回结果
if (attr != null && !string.IsNullOrEmpty(attr.Description))
return attr.Description;
else
return string.Empty;
}
catch (Exception)
{
return string.Empty;
}
}
///
/// 返回枚举项的描述信息。
///
/// 要获取描述信息的枚举项。
/// 枚举项的描述信息。
public static string GetEnumDesc(Enum e)
{
if (e == null)
{
return string.Empty;
}
Type enumType = e.GetType();
DescriptionAttribute attr = null;
// 获取枚举字段。
FieldInfo fieldInfo = enumType.GetField(e.ToString());
if (fieldInfo != null)
{
// 获取描述的属性。
attr = Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute), false) as DescriptionAttribute;
}
// 返回结果
if (attr != null && !string.IsNullOrEmpty(attr.Description))
return attr.Description;
else
return string.Empty;
}
///
/// 获取枚举描述列表,并转化为键值对
///
///
/// 是否包含“全部”
/// 过滤项
///
public static List EnumDescToList(bool isHasAll, params string[] filterItem)
{
List list = new List();
// 如果包含全部则添加
if (isHasAll)
{
list.Add(new EnumKeyValue() { Key = 0, Name = "全部" });
}
#region 方式一
//foreach (var item in typeof(T).GetFields())
//{
// // 获取描述
// var attr = item.GetCustomAttribute(typeof(DescriptionAttribute), true) as DescriptionAttribute;
// if (attr != null && !string.IsNullOrEmpty(attr.Description))
// {
// // 跳过过滤项
// if (Array.IndexOf(filterItem, attr.Description) != -1)
// {
// continue;
// }
// // 添加
// EnumKeyValue model = new EnumKeyValue();
// model.Key = (int)Enum.Parse(typeof(T), item.Name);
// model.Name = attr.Description;
// list.Add(model);
// }
//}
#endregion
#region 方式二
//foreach (int item in Enum.GetValues(typeof(T)))
//{
// // 获取描述
// FieldInfo fi = typeof(T).GetField(Enum.GetName(typeof(T), item));
// var attr = fi.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
// if (attr != null && !string.IsNullOrEmpty(attr.Description))
// {
// // 跳过过滤项
// if (Array.IndexOf(filterItem, attr.Description) != -1)
// {
// continue;
// }
// // 添加
// EnumKeyValue model = new EnumKeyValue();
// model.Key = item;
// model.Name = attr.Description;
// list.Add(model);
// }
//}
#endregion
return list;
}
///
/// 获取枚举值列表,并转化为键值对
///
///
/// 是否包含“全部”
/// 过滤项
///
public static List EnumToList(bool isHasAll, params string[] filterItem)
{
List list = new List();
// 如果包含全部则添加
if (isHasAll)
{
list.Add(new EnumKeyValue() { Key = -1, Name = "全部" });
}
foreach (int item in Enum.GetValues(typeof(T)))
{
string name = Enum.GetName(typeof(T), item);
// 跳过过滤项
if (Array.IndexOf(filterItem, name) != -1)
{
continue;
}
// 添加
EnumKeyValue model = new EnumKeyValue();
model.Key = item;
model.Name = name;
model.Desc = EnumHelper.GetEnumDesc(item);
list.Add(model);
}
return list;
}
}
///
/// 枚举键值对
///
public class EnumKeyValue
{
public int Key { get; set; }
public string Desc { get; set; }
public string Name { get; set; }
}
}