标签:xenu man rdo 类型 info core new too 枚举
定义枚举类型:
internal enum SexEnum
{
[Description("男性")]
Man=1,
[Description("女性")]
Woman =2,
[Description("未知")]
Unknown =3,
TestNoDescription=4
}
枚举扩展方法:
/// <summary>
/// 枚举<see cref="Enum" />的扩展辅助操作方法
/// </summary>
public static class EnumExtensions
{
/// <summary>
/// 获取枚举项上的<see cref="DescriptionAttribute" />特性的文字描述
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ToDescription(this Enum value)
{
var type = value.GetType();
var member = type.GetMember(value.ToString()).FirstOrDefault();
return member != null ? member.ToDescription() : value.ToString();
}
/// <summary>
/// 获取成员元数据的Description特性描述信息
/// </summary>
/// <param name="member">成员元数据对象</param>
/// <param name="inherit">是否搜索成员的继承链以查找描述特性</param>
/// <returns>返回Description特性描述信息,如不存在则返回成员的名称</returns>
public static string ToDescription(this MemberInfo member, bool inherit = false)
{
var desc = member.GetAttribute<DescriptionAttribute>(inherit);
return desc == null ? member.Name : desc.Description;
}
/// <summary>
/// 从类型成员获取指定Attribute特性
/// </summary>
/// <typeparam name="T">Attribute特性类型</typeparam>
/// <param name="memberInfo">类型类型成员</param>
/// <param name="inherit">是否从继承中查找</param>
/// <returns>存在返回第一个,不存在返回null</returns>
public static T GetAttribute<T>(this MemberInfo memberInfo, bool inherit = false) where T : Attribute
{
var descripts = memberInfo.GetCustomAttributes(typeof(T), inherit);
//获取第一个DescriptionAttribute
return descripts.FirstOrDefault() as T;
}
}
获得枚举信息:
List<EnumInfo> result = new List<EnumInfo>();
var list = Enum.GetValues(typeof(SexEnum)).Cast<SexEnum>();
Console.WriteLine($"枚举结果如下:");
foreach (var item in list)
{
var info = new EnumInfo() { Name = item.ToString(), Value = (int)item, Description = item.ToDescription() };
result.Add(info);
Console.WriteLine($"Name:{info.Name},Value:{info.Value},Description:{info.Description}");
}
标签:xenu man rdo 类型 info core new too 枚举
原文地址:https://www.cnblogs.com/Lulus/p/12520478.html