Skip to content

Commit 90e19ff

Browse files
committed
CSHARP-5632: Requested changes
1 parent 1c99be2 commit 90e19ff

File tree

1 file changed

+22
-71
lines changed

1 file changed

+22
-71
lines changed

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

Lines changed: 22 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace MongoDB.Driver.Linq.Linq3Implementation.Misc
2424
{
2525
internal static class TypeExtensions
2626
{
27-
private static readonly Type[] __dictionaryInterfaces =
27+
private static readonly Type[] __dictionaryInterfaceDefinitions =
2828
{
2929
typeof(IDictionary<,>),
3030
typeof(IReadOnlyDictionary<,>)
@@ -102,7 +102,7 @@ public static bool Implements(this Type type, Type @interface)
102102

103103
public static bool ImplementsDictionaryInterface(this Type type, out Type keyType, out Type valueType)
104104
{
105-
if (TryGetGenericInterface(type, __dictionaryInterfaces, out var dictionaryInterface))
105+
if (TryGetGenericInterface(type, __dictionaryInterfaceDefinitions, out var dictionaryInterface))
106106
{
107107
var genericArguments = dictionaryInterface.GetGenericArguments();
108108
keyType = genericArguments[0];
@@ -226,11 +226,9 @@ public static bool IsEnum(this Type type, out Type underlyingType)
226226
underlyingType = Enum.GetUnderlyingType(type);
227227
return true;
228228
}
229-
else
230-
{
231-
underlyingType = null;
232-
return false;
233-
}
229+
230+
underlyingType = null;
231+
return false;
234232
}
235233

236234
public static bool IsEnumOrNullableEnum(this Type type, out Type enumType, out Type underlyingType)
@@ -256,11 +254,9 @@ public static bool IsNullable(this Type type, out Type valueType)
256254
valueType = type.GetGenericArguments()[0];
257255
return true;
258256
}
259-
else
260-
{
261-
valueType = null;
262-
return false;
263-
}
257+
258+
valueType = null;
259+
return false;
264260
}
265261

266262
public static bool IsNullableEnum(this Type type)
@@ -366,77 +362,32 @@ public static bool IsValueTuple(this Type type)
366362
__valueTupleTypeDefinitions.Contains(typeDefinition);
367363
}
368364

369-
public static bool TryGetGenericInterface(this Type type, Type[] interfaceDefinitions, out Type genericInterface)
365+
public static bool TryGetGenericInterface(this Type type, Type genericInterfaceDefintion, out Type genericInterface)
370366
{
371367
genericInterface =
372-
type.IsConstructedGenericType && interfaceDefinitions.Contains(type.GetGenericTypeDefinition()) ?
368+
type.IsConstructedGenericType && type.GetGenericTypeDefinition() == genericInterfaceDefintion ?
373369
type :
374-
type.GetInterfaces().FirstOrDefault(i => i.IsConstructedGenericType && interfaceDefinitions.Contains(i.GetGenericTypeDefinition()));
370+
type.GetInterfaces().FirstOrDefault(i => i.IsConstructedGenericType && i.GetGenericTypeDefinition() == genericInterfaceDefintion);
375371
return genericInterface != null;
376372
}
377373

378-
public static bool TryGetIEnumerableGenericInterface(this Type type, out Type ienumerableGenericInterface)
374+
public static bool TryGetGenericInterface(this Type type, Type[] genericInterfaceDefinitions, out Type genericInterface)
379375
{
380-
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
381-
{
382-
ienumerableGenericInterface = type;
383-
return true;
384-
}
385-
386-
foreach (var interfaceType in type.GetInterfaces())
387-
{
388-
if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
389-
{
390-
ienumerableGenericInterface = interfaceType;
391-
return true;
392-
}
393-
}
394-
395-
ienumerableGenericInterface = null;
396-
return false;
376+
genericInterface =
377+
type.IsConstructedGenericType && genericInterfaceDefinitions.Contains(type.GetGenericTypeDefinition()) ?
378+
type :
379+
type.GetInterfaces().FirstOrDefault(i => i.IsConstructedGenericType && genericInterfaceDefinitions.Contains(i.GetGenericTypeDefinition()));
380+
return genericInterface != null;
397381
}
398382

399-
public static bool TryGetIListGenericInterface(this Type type, out Type ilistGenericInterface)
400-
{
401-
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IList<>))
402-
{
403-
ilistGenericInterface = type;
404-
return true;
405-
}
406-
407-
foreach (var interfaceType in type.GetInterfaces())
408-
{
409-
if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IList<>))
410-
{
411-
ilistGenericInterface = interfaceType;
412-
return true;
413-
}
414-
}
383+
public static bool TryGetIEnumerableGenericInterface(this Type type, out Type ienumerableGenericInterface)
384+
=> TryGetGenericInterface(type, typeof(IEnumerable<>), out ienumerableGenericInterface);
415385

416-
ilistGenericInterface = null;
417-
return false;
418-
}
386+
public static bool TryGetIListGenericInterface(this Type type, out Type ilistGenericInterface)
387+
=> TryGetGenericInterface(type, typeof(IList<>), out ilistGenericInterface);
419388

420389
public static bool TryGetIQueryableGenericInterface(this Type type, out Type iqueryableGenericInterface)
421-
{
422-
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IQueryable<>))
423-
{
424-
iqueryableGenericInterface = type;
425-
return true;
426-
}
427-
428-
foreach (var interfaceType in type.GetInterfaces())
429-
{
430-
if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IQueryable<>))
431-
{
432-
iqueryableGenericInterface = interfaceType;
433-
return true;
434-
}
435-
}
436-
437-
iqueryableGenericInterface = null;
438-
return false;
439-
}
390+
=> TryGetGenericInterface(type, typeof(IQueryable<>), out iqueryableGenericInterface);
440391

441392
private static TValue GetDefaultValueGeneric<TValue>()
442393
{

0 commit comments

Comments
 (0)