Skip to content

Commit

Permalink
Fix method handler being unable to unwrap derived task instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Feb 26, 2024
1 parent 62557af commit a647db6
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions Modules/Reflection/MethodHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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

}
Expand Down

0 comments on commit a647db6

Please sign in to comment.