MethodInfoExtension.cs 664 B

12345678910111213141516171819202122
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. namespace ZhonTai.DynamicApi;
  6. public static class MethodInfoExtension
  7. {
  8. public static bool IsAsync(this MethodInfo method)
  9. {
  10. return method.ReturnType == typeof(Task)
  11. || (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition() == typeof(Task<>));
  12. }
  13. internal static Type GetReturnType(this MethodInfo method)
  14. {
  15. var isAsync = method.IsAsync();
  16. var returnType = method.ReturnType;
  17. return isAsync ? (returnType.GenericTypeArguments.FirstOrDefault() ?? typeof(void)) : returnType;
  18. }
  19. }