ReflectionUtil.cs 651 B

12345678910111213141516171819202122
  1. namespace Admin.NET.Core;
  2. /// <summary>
  3. /// 反射工具类
  4. /// </summary>
  5. public static class ReflectionUtil
  6. {
  7. /// <summary>
  8. /// 获取字段特性
  9. /// </summary>
  10. /// <param name="field"></param>
  11. /// <typeparam name="T"></typeparam>
  12. /// <returns></returns>
  13. public static T GetDescriptionValue<T>(this FieldInfo field) where T : Attribute
  14. {
  15. // 获取字段的指定特性,不包含继承中的特性
  16. object[] customAttributes = field.GetCustomAttributes(typeof(T), false);
  17. // 如果没有数据返回null
  18. return customAttributes.Length > 0 ? (T)customAttributes[0] : null;
  19. }
  20. }