Enum 枚举值 (一) 获取描述信息

时间:2021-03-01 16:01:54

封装了方法:

public static class EnumOperate
{
public class BaseDescriptionAttribute : DescriptionAttribute
{
public BaseDescriptionAttribute(string descriptionCN)
: base(descriptionCN)
{
}
public BaseDescriptionAttribute(string descriptionCN, string descriptionEN)
: base(descriptionCN)
{
this.descriptionEN = descriptionEN;
} private string descriptionEN; public string DescriptionEN
{
get { return descriptionEN; }
set { descriptionEN = value; }
} } /// <summary>
/// 扩展方法,获得枚举的Description中英文
public static EnumItem GetBaseDescription(this Enum value, Boolean nameInstead = true)
{
EnumItem e = new EnumItem(); Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
} FieldInfo field = type.GetField(name);
BaseDescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(BaseDescriptionAttribute)) as BaseDescriptionAttribute; e.EnumKey = Convert.ToInt32(value);
e.EnumValue = value.ToString();
if (attribute == null && nameInstead == true)
{
e.DescriptionCN = name;
e.DescriptionEN = name;
return e;
}
else
{
e.DescriptionCN = attribute.Description;
e.DescriptionEN = attribute.DescriptionEN;
return e;
} } /// <summary>
/// 扩展方法,获得枚举的Description
/// </summary>
/// <param name="value">枚举值</param>
/// <param name="nameInstead">当枚举值没有定义DescriptionAttribute,是否使用枚举名代替,默认是使用</param>
/// <returns>枚举的Description</returns>
public static string GetDescription(this Enum value, Boolean nameInstead = true)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name == null)
{
return null;
} FieldInfo field = type.GetField(name);
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; if (attribute == null && nameInstead == true)
{
return name;
}
return attribute == null ? null : attribute.Description;
} /// <summary>
/// 把枚举转换为键值对集合
/// 枚举转换为键值对集合
/// Dictionary<Int32, String> dic = EnumUtil.EnumToDictionary(typeof(Season), e => e.GetDescription());
/// </summary>
/// <param name="enumType">枚举类型</param>
/// <param name="getText">获得值得文本</param>
/// <returns>以枚举值为key,枚举文本为value的键值对集合</returns>
public static Dictionary<Int32, String> EnumToDictionary(Type enumType, Func<Enum, String> getText)
{
if (!enumType.IsEnum)
{
throw new ArgumentException("传入的参数必须是枚举类型!", "enumType");
}
Dictionary<Int32, String> enumDic = new Dictionary<int, string>();
Array enumValues = Enum.GetValues(enumType);
foreach (Enum enumValue in enumValues)
{
Int32 key = Convert.ToInt32(enumValue);
String value = getText(enumValue);
enumDic.Add(key, value);
}
return enumDic;
} /// <summary>
/// 获取枚举的相关信息
/// </summary>
/// <param name="e">枚举的类型</param>
/// <returns></returns>
public static List<EnumItem> GetEnumItems(Type e)
{
List<EnumItem> itemList = new List<EnumItem>();
foreach (Enum v in Enum.GetValues(e))
{
EnumItem item = new EnumItem();
// TODO: 遍历操作
var q=GetBaseDescription(v);
item.EnumKey = q.EnumKey;
item.EnumValue = q.EnumValue;
item.DescriptionCN = q.DescriptionCN;
item.DescriptionEN = q.DescriptionEN;
itemList.Add(item);
}
return itemList; } public class EnumItem
{
public int EnumKey { get; set; }
public string EnumValue { get; set; }
public string DescriptionCN { get; set; }
public string DescriptionEN { get; set; }
}
}

枚举:

//港口类型
public enum HarborType
{
[Description("Seaport")]
[EnumOperate.BaseDescription("Seaport", "海港")]
Seaport = 0, [Description("Airport")]
[EnumOperate.BaseDescription("Airport", "空港")]
Airport = 1,
}

使用:

static void Main(string[] args)
{
//获取枚举信息
var q = EnumOperate.GetEnumItems(typeof(HarborType)); Console.ReadLine();
}