From a647db6b8066e3e9a2c9a39d133ba1184db8f501 Mon Sep 17 00:00:00 2001 From: Kaliumhexacyanoferrat Date: Mon, 26 Feb 2024 14:17:33 +0100 Subject: [PATCH] Fix method handler being unable to unwrap derived task instances --- Modules/Reflection/MethodHandler.cs | 30 +++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/Modules/Reflection/MethodHandler.cs b/Modules/Reflection/MethodHandler.cs index d115826c..25097d43 100644 --- a/Modules/Reflection/MethodHandler.cs +++ b/Modules/Reflection/MethodHandler.cs @@ -396,9 +396,8 @@ private bool TryResolvePath(ContentHint input, [MaybeNullWhen(returnValue: false } var type = result.GetType(); - var genericType = (type.IsGenericType) ? type.GetGenericTypeDefinition() : null; - if (genericType == typeof(ValueTask<>) || genericType == typeof(Task<>)) + if (IsAssignableToGenericType(type, typeof(ValueTask<>)) || IsAssignableToGenericType(type, typeof(Task<>))) { dynamic task = result; @@ -423,6 +422,33 @@ private bool TryResolvePath(ContentHint input, [MaybeNullWhen(returnValue: false return result; } + private static bool IsAssignableToGenericType(Type givenType, Type genericType) + { + var interfaceTypes = givenType.GetInterfaces(); + + foreach (var it in interfaceTypes) + { + if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType) + { + return true; + } + } + + if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType) + { + return true; + } + + var baseType = givenType.BaseType; + + if (baseType == null) + { + return false; + } + + return IsAssignableToGenericType(baseType, genericType); + } + #endregion }