Skip to content

Commit 9ed7fa3

Browse files
committed
MethodSignatureComparer.Equals should be commutative
So far, we simply assumed that `x` := override method, and `y` := over- ridden method, but there is actually no guarantee for that.
1 parent 4bc7ba2 commit 9ed7fa3

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/Castle.Core/DynamicProxy/Generators/MethodSignatureComparer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public bool EqualParameters(MethodInfo x, MethodInfo y)
7979
return true;
8080
}
8181

82-
public bool EqualSignatureTypes(Type x, Type y, MethodInfo xm = null)
82+
public bool EqualSignatureTypes(Type x, Type y, MethodInfo xm = null, MethodInfo ym = null)
8383
{
8484
if (x.IsGenericParameter != y.IsGenericParameter)
8585
{
@@ -127,9 +127,10 @@ public bool EqualSignatureTypes(Type x, Type y, MethodInfo xm = null)
127127
// This enables covariant method returns for .NET 5 and newer.
128128
// No need to check for runtime support, since such methods are marked with a custom attribute;
129129
// see https://github.com/dotnet/runtime/blob/main/docs/design/features/covariant-return-methods.md.
130-
if (xm != null && preserveBaseOverridesAttribute != null && xm.IsDefined(preserveBaseOverridesAttribute, inherit: false))
130+
if (preserveBaseOverridesAttribute != null)
131131
{
132-
return y.IsAssignableFrom(x);
132+
return (xm != null && xm.IsDefined(preserveBaseOverridesAttribute, inherit: false) && y.IsAssignableFrom(x))
133+
|| (ym != null && ym.IsDefined(preserveBaseOverridesAttribute, inherit: false) && x.IsAssignableFrom(y));
133134
}
134135

135136
return false;
@@ -152,7 +153,7 @@ public bool Equals(MethodInfo x, MethodInfo y)
152153

153154
return EqualNames(x, y) &&
154155
EqualGenericParameters(x, y) &&
155-
EqualSignatureTypes(x.ReturnType, y.ReturnType, x) &&
156+
EqualSignatureTypes(x.ReturnType, y.ReturnType, x, y) &&
156157
EqualParameters(x, y);
157158
}
158159

0 commit comments

Comments
 (0)