ReflectionUtil.cs 827 B

1234567891011121314151617181920212223242526
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. namespace Admin.NET.Core;
  5. /// <summary>
  6. /// 反射工具类
  7. /// </summary>
  8. public static class ReflectionUtil
  9. {
  10. /// <summary>
  11. /// 获取字段特性
  12. /// </summary>
  13. /// <param name="field"></param>
  14. /// <typeparam name="T"></typeparam>
  15. /// <returns></returns>
  16. public static T GetDescriptionValue<T>(this FieldInfo field) where T : Attribute
  17. {
  18. // 获取字段的指定特性,不包含继承中的特性
  19. object[] customAttributes = field.GetCustomAttributes(typeof(T), false);
  20. // 如果没有数据返回null
  21. return customAttributes.Length > 0 ? (T)customAttributes[0] : null;
  22. }
  23. }