1
0

MethodInfoExtensions.cs 810 B

1234567891011121314151617181920212223242526
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. namespace Admin.Core.Common.Extensions
  6. {
  7. public static class MethodInfoExtensions
  8. {
  9. public static bool HasAttribute<T>(this MethodInfo method)
  10. {
  11. return method.GetCustomAttributes(typeof(T), false).FirstOrDefault() is T;
  12. }
  13. public static T GetAttribute<T>(this MethodInfo method) where T : Attribute
  14. {
  15. return method.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
  16. }
  17. public static bool IsAsync(this MethodInfo method)
  18. {
  19. return method.ReturnType == typeof(Task)
  20. || (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>));
  21. }
  22. }
  23. }