Skip to content

Commit

Permalink
Also check for covariant return types if one of them is generic
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Dec 29, 2022
1 parent 06884f4 commit 6238c15
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/Castle.Core/DynamicProxy/Generators/MethodSignatureComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private bool EqualSignatureTypes(Type x, Type y, MethodInfo xm = null, MethodInf
}
else if (x.IsGenericType != y.IsGenericType)
{
return false;
return IsCovariantReturnTypes(x, y, xm, ym);
}

if (x.IsGenericParameter)
Expand Down Expand Up @@ -129,19 +129,24 @@ private bool EqualSignatureTypes(Type x, Type y, MethodInfo xm = null, MethodInf
{
if (!x.Equals(y))
{
// This enables covariant method returns for .NET 5 and newer.
// No need to check for runtime support, since such methods are marked with a custom attribute;
// see https://github.com/dotnet/runtime/blob/main/docs/design/features/covariant-return-methods.md.
if (preserveBaseOverridesAttribute != null)
{
return (xm != null && xm.IsDefined(preserveBaseOverridesAttribute, inherit: false) && y.IsAssignableFrom(x))
|| (ym != null && ym.IsDefined(preserveBaseOverridesAttribute, inherit: false) && x.IsAssignableFrom(y));
}

return false;
return IsCovariantReturnTypes(x, y, xm, ym);
}
}
return true;

static bool IsCovariantReturnTypes(Type x, Type y, MethodInfo xm, MethodInfo ym)
{
// This enables covariant method returns for .NET 5 and newer.
// No need to check for runtime support, since such methods are marked with a custom attribute;
// see https://github.com/dotnet/runtime/blob/main/docs/design/features/covariant-return-methods.md.
if (preserveBaseOverridesAttribute != null)
{
return (xm != null && xm.IsDefined(preserveBaseOverridesAttribute, inherit: false) && y.IsAssignableFrom(x))
|| (ym != null && ym.IsDefined(preserveBaseOverridesAttribute, inherit: false) && x.IsAssignableFrom(y));
}

return false;
}
}

public bool Equals(MethodInfo x, MethodInfo y)
Expand Down

0 comments on commit 6238c15

Please sign in to comment.