Skip to content

Commit 0d10969

Browse files
committed
CSHARP-5730: Comitting a file that somehow got left out of the previous commit.
1 parent bc8cb71 commit 0d10969

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Misc/MethodInfoExtensions.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
17+
using System.Linq;
1618
using System.Reflection;
1719

1820
namespace MongoDB.Driver.Linq.Linq3Implementation.Misc
@@ -40,15 +42,26 @@ public static bool Is(this MethodInfo method, MethodInfo comparand)
4042

4143
public static bool IsInstanceCompareToMethod(this MethodInfo method)
4244
{
43-
return
44-
method.IsPublic &&
45+
if (method.IsPublic &&
4546
!method.IsStatic &&
4647
method.ReturnType == typeof(int) &&
4748
method.Name == "CompareTo" &&
4849
method.GetParameters() is var parameters &&
49-
parameters.Length == 1 &&
50-
parameters[0].ParameterType is var parameterType &&
51-
(parameterType == method.DeclaringType || parameterType == typeof(object));
50+
parameters.Length == 1)
51+
{
52+
var declaringType = method.DeclaringType;
53+
var comparandType = declaringType switch
54+
{
55+
_ when declaringType == typeof(IComparable) => typeof(object),
56+
_ when declaringType.IsConstructedGenericType && declaringType.GetGenericTypeDefinition() == typeof(IComparable<>) => declaringType.GetGenericArguments().Single(),
57+
_ => declaringType
58+
};
59+
60+
var parameterType = parameters[0].ParameterType;
61+
return parameterType == comparandType;
62+
}
63+
64+
return false;
5265
}
5366

5467
public static bool IsOneOf(this MethodInfo method, MethodInfo comparand1, MethodInfo comparand2)

0 commit comments

Comments
 (0)