Skip to content

Commit 9981031

Browse files
committed
CSHARP-5632: Requested changes
1 parent 7af6c03 commit 9981031

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)
@@ -350,77 +346,32 @@ public static bool IsValueTuple(this Type type)
350346
__valueTupleTypeDefinitions.Contains(typeDefinition);
351347
}
352348

353-
public static bool TryGetGenericInterface(this Type type, Type[] interfaceDefinitions, out Type genericInterface)
349+
public static bool TryGetGenericInterface(this Type type, Type genericInterfaceDefintion, out Type genericInterface)
354350
{
355351
genericInterface =
356-
type.IsConstructedGenericType && interfaceDefinitions.Contains(type.GetGenericTypeDefinition()) ?
352+
type.IsConstructedGenericType && type.GetGenericTypeDefinition() == genericInterfaceDefintion ?
357353
type :
358-
type.GetInterfaces().FirstOrDefault(i => i.IsConstructedGenericType && interfaceDefinitions.Contains(i.GetGenericTypeDefinition()));
354+
type.GetInterfaces().FirstOrDefault(i => i.IsConstructedGenericType && i.GetGenericTypeDefinition() == genericInterfaceDefintion);
359355
return genericInterface != null;
360356
}
361357

362-
public static bool TryGetIEnumerableGenericInterface(this Type type, out Type ienumerableGenericInterface)
358+
public static bool TryGetGenericInterface(this Type type, Type[] genericInterfaceDefinitions, out Type genericInterface)
363359
{
364-
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
365-
{
366-
ienumerableGenericInterface = type;
367-
return true;
368-
}
369-
370-
foreach (var interfaceType in type.GetInterfaces())
371-
{
372-
if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IEnumerable<>))
373-
{
374-
ienumerableGenericInterface = interfaceType;
375-
return true;
376-
}
377-
}
378-
379-
ienumerableGenericInterface = null;
380-
return false;
360+
genericInterface =
361+
type.IsConstructedGenericType && genericInterfaceDefinitions.Contains(type.GetGenericTypeDefinition()) ?
362+
type :
363+
type.GetInterfaces().FirstOrDefault(i => i.IsConstructedGenericType && genericInterfaceDefinitions.Contains(i.GetGenericTypeDefinition()));
364+
return genericInterface != null;
381365
}
382366

383-
public static bool TryGetIListGenericInterface(this Type type, out Type ilistGenericInterface)
384-
{
385-
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IList<>))
386-
{
387-
ilistGenericInterface = type;
388-
return true;
389-
}
390-
391-
foreach (var interfaceType in type.GetInterfaces())
392-
{
393-
if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IList<>))
394-
{
395-
ilistGenericInterface = interfaceType;
396-
return true;
397-
}
398-
}
367+
public static bool TryGetIEnumerableGenericInterface(this Type type, out Type ienumerableGenericInterface)
368+
=> TryGetGenericInterface(type, typeof(IEnumerable<>), out ienumerableGenericInterface);
399369

400-
ilistGenericInterface = null;
401-
return false;
402-
}
370+
public static bool TryGetIListGenericInterface(this Type type, out Type ilistGenericInterface)
371+
=> TryGetGenericInterface(type, typeof(IList<>), out ilistGenericInterface);
403372

404373
public static bool TryGetIQueryableGenericInterface(this Type type, out Type iqueryableGenericInterface)
405-
{
406-
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IQueryable<>))
407-
{
408-
iqueryableGenericInterface = type;
409-
return true;
410-
}
411-
412-
foreach (var interfaceType in type.GetInterfaces())
413-
{
414-
if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IQueryable<>))
415-
{
416-
iqueryableGenericInterface = interfaceType;
417-
return true;
418-
}
419-
}
420-
421-
iqueryableGenericInterface = null;
422-
return false;
423-
}
374+
=> TryGetGenericInterface(type, typeof(IQueryable<>), out iqueryableGenericInterface);
424375

425376
private static TValue GetDefaultValueGeneric<TValue>()
426377
{

0 commit comments

Comments
 (0)