Skip to content

Commit a647db6

Browse files
Fix method handler being unable to unwrap derived task instances
1 parent 62557af commit a647db6

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
@@ -396,9 +396,8 @@ private bool TryResolvePath(ContentHint input, [MaybeNullWhen(returnValue: false
396396
}
397397

398398
var type = result.GetType();
399-
var genericType = (type.IsGenericType) ? type.GetGenericTypeDefinition() : null;
400399

401-
if (genericType == typeof(ValueTask<>) || genericType == typeof(Task<>))
400+
if (IsAssignableToGenericType(type, typeof(ValueTask<>)) || IsAssignableToGenericType(type, typeof(Task<>)))
402401
{
403402
dynamic task = result;
404403

@@ -423,6 +422,33 @@ private bool TryResolvePath(ContentHint input, [MaybeNullWhen(returnValue: false
423422
return result;
424423
}
425424

425+
private static bool IsAssignableToGenericType(Type givenType, Type genericType)
426+
{
427+
var interfaceTypes = givenType.GetInterfaces();
428+
429+
foreach (var it in interfaceTypes)
430+
{
431+
if (it.IsGenericType && it.GetGenericTypeDefinition() == genericType)
432+
{
433+
return true;
434+
}
435+
}
436+
437+
if (givenType.IsGenericType && givenType.GetGenericTypeDefinition() == genericType)
438+
{
439+
return true;
440+
}
441+
442+
var baseType = givenType.BaseType;
443+
444+
if (baseType == null)
445+
{
446+
return false;
447+
}
448+
449+
return IsAssignableToGenericType(baseType, genericType);
450+
}
451+
426452
#endregion
427453

428454
}

0 commit comments

Comments
 (0)