C#获取枚举类型字段的特性(Attribute)
C# 全选
/// <summary>
/// 获取枚举类型指定字段的特性(Attribute)
/// </summary>
/// <typeparam name="T">特性(Attribute)类型</typeparam>
/// <param name="enumType">枚举类型</param>
/// <param name="fieldName">字段名称</param>
/// <returns></returns>
public static T GetEnumFieldAttribute<T>(Type enumType, string fieldName) where T : Attribute
{
if (Enum.IsDefined(enumType, fieldName))
{
var field = enumType.GetField(fieldName, BindingFlags.Static | BindingFlags.Public);
return field.GetCustomAttribute<T>();
}
else
{
return default(T);
}
}
使用:
C# 全选
var attr = ObjectHelper.GetEnumFieldAttribute<DescriptionAttribute>(typeof(DrawNodeType), node.ObjectType.ToString());
return attr.Description;
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网