public Type GetGenericInterface(Type type, Type interfaceType)
{
if (!interfaceType.GetTypeInfo().IsGenericTypeDefinition)
{
throw new ArgumentException(
"The interface must be a generic interface definition: " + interfaceType.Name,
nameof(interfaceType));
}
// our contract states that we will not return generic interface definitions without generic type arguments
if (type == interfaceType)
return null;
if (type.GetTypeInfo().IsGenericType)
{
if (type.GetGenericTypeDefinition() == interfaceType)
return type;
}
Type[] interfaces = type.GetTypeInfo().ImplementedInterfaces.ToArray();
return interfaces.Where(t => t.GetTypeInfo().IsGenericType)
.FirstOrDefault(t => t.GetGenericTypeDefinition() == interfaceType);
}