Skip to content

Commit daf2124

Browse files
committed
CSHARP-1326: Fix edge case in Implements and TryGetIEnumerableGenericInterface.
1 parent f5c2cbf commit daf2124

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ public static Type GetIEnumerableGenericInterface(this Type enumerableType)
3232

3333
public static bool Implements(this Type type, Type @interface)
3434
{
35-
Type interfaceDefinition = null;
36-
if (@interface.IsGenericType())
35+
if (type == @interface)
3736
{
38-
interfaceDefinition = @interface.GetGenericTypeDefinition();
37+
return true;
38+
}
39+
40+
if (type.IsGenericType() && type.GetGenericTypeDefinition() == @interface)
41+
{
42+
return true;
3943
}
4044

4145
foreach (var implementedInterface in type.GetInterfaces())
@@ -45,7 +49,7 @@ public static bool Implements(this Type type, Type @interface)
4549
return true;
4650
}
4751

48-
if (implementedInterface.IsGenericType() && implementedInterface.GetGenericTypeDefinition() == interfaceDefinition)
52+
if (implementedInterface.IsGenericType() && implementedInterface.GetGenericTypeDefinition() == @interface)
4953
{
5054
return true;
5155
}
@@ -104,6 +108,12 @@ public static bool TryGetIDictionaryGenericInterface(this Type type, out Type id
104108

105109
public static bool TryGetIEnumerableGenericInterface(this Type type, out Type ienumerableGenericInterface)
106110
{
111+
if (type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
112+
{
113+
ienumerableGenericInterface = type;
114+
return true;
115+
}
116+
107117
foreach (var interfaceType in type.GetInterfaces())
108118
{
109119
if (interfaceType.IsGenericType() && interfaceType.GetGenericTypeDefinition() == typeof(IEnumerable<>))

0 commit comments

Comments
 (0)