Skip to content

Commit 8ea2994

Browse files
Fix method handler being unable to unwrap derived task instances
1 parent 59315c3 commit 8ea2994

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

Modules/Reflection/MethodHandler.cs

+28-2
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,8 @@ private bool TryResolvePath(ContentHint input, [MaybeNullWhen(returnValue: false
419419
}
420420

421421
var type = result.GetType();
422-
var genericType = (type.IsGenericType) ? type.GetGenericTypeDefinition() : null;
423422

424-
if (genericType == typeof(ValueTask<>) || genericType == typeof(Task<>))
423+
if (IsAssignableToGenericType(type, typeof(ValueTask<>)) || IsAssignableToGenericType(type, typeof(Task<>)))
425424
{
426425
dynamic task = result;
427426

@@ -446,6 +445,33 @@ private bool TryResolvePath(ContentHint input, [MaybeNullWhen(returnValue: false
446445
return result;
447446
}
448447

448+
private static bool IsAssignableToGenericType(Type givenType, Type genericType)
449+
{
450+
var interfaceTypes = givenType.GetInterfaces();
451+
452+
foreach (var it in interfaceTypes)
453+
{
454+
if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)
455+
{
456+
return true;
457+
}
458+
}
459+
460+
if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
461+
{
462+
return true;
463+
}
464+
465+
var baseType = givenType.BaseType;
466+
467+
if (baseType == null)
468+
{
469+
return false;
470+
}
471+
472+
return IsAssignableToGenericType(baseType, genericType);
473+
}
474+
449475
#endregion
450476

451477
}

0 commit comments

Comments
 (0)