@@ -81,10 +81,27 @@ public bool EqualParameters(MethodInfo x, MethodInfo y)
8181
8282 public bool EqualReturnTypes ( MethodInfo x , MethodInfo y )
8383 {
84- return EqualSignatureTypes ( x . ReturnType , y . ReturnType , x , y ) ;
84+ var xr = x . ReturnType ;
85+ var yr = y . ReturnType ;
86+
87+ if ( EqualSignatureTypes ( xr , yr ) )
88+ {
89+ return true ;
90+ }
91+
92+ // This enables covariant method returns for .NET 5 and newer.
93+ // No need to check for runtime support, since such methods are marked with a custom attribute;
94+ // see https://github.com/dotnet/runtime/blob/main/docs/design/features/covariant-return-methods.md.
95+ if ( preserveBaseOverridesAttribute != null )
96+ {
97+ return ( x . IsDefined ( preserveBaseOverridesAttribute , inherit : false ) && yr . IsAssignableFrom ( xr ) )
98+ || ( y . IsDefined ( preserveBaseOverridesAttribute , inherit : false ) && xr . IsAssignableFrom ( yr ) ) ;
99+ }
100+
101+ return false ;
85102 }
86103
87- private bool EqualSignatureTypes ( Type x , Type y , MethodInfo xm = null , MethodInfo ym = null )
104+ private bool EqualSignatureTypes ( Type x , Type y )
88105 {
89106 if ( x . IsGenericParameter != y . IsGenericParameter )
90107 {
@@ -122,22 +139,13 @@ private bool EqualSignatureTypes(Type x, Type y, MethodInfo xm = null, MethodInf
122139
123140 for ( var i = 0 ; i < xArgs . Length ; ++ i )
124141 {
125- if ( ! EqualSignatureTypes ( xArgs [ i ] , yArgs [ i ] ) ) return false ;
142+ if ( ! EqualSignatureTypes ( xArgs [ i ] , yArgs [ i ] ) ) return false ;
126143 }
127144 }
128145 else
129146 {
130147 if ( ! x . Equals ( y ) )
131148 {
132- // This enables covariant method returns for .NET 5 and newer.
133- // No need to check for runtime support, since such methods are marked with a custom attribute;
134- // see https://github.com/dotnet/runtime/blob/main/docs/design/features/covariant-return-methods.md.
135- if ( preserveBaseOverridesAttribute != null )
136- {
137- return ( xm != null && xm . IsDefined ( preserveBaseOverridesAttribute , inherit : false ) && y . IsAssignableFrom ( x ) )
138- || ( ym != null && ym . IsDefined ( preserveBaseOverridesAttribute , inherit : false ) && x . IsAssignableFrom ( y ) ) ;
139- }
140-
141149 return false ;
142150 }
143151 }
0 commit comments