internal static Type FindGenericType(Type definition, Type type)
{
bool? definitionIsInterface = null;
while (type != null && type != typeof(object))
{
TypeInfo typeInfo = type.GetTypeInfo();
if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == definition)
return type;
if (!definitionIsInterface.HasValue)
definitionIsInterface = definition.GetTypeInfo().IsInterface;
if (definitionIsInterface.GetValueOrDefault())
{
foreach (Type itype in typeInfo.ImplementedInterfaces)
{
Type found = FindGenericType(definition, itype);
if (found != null)
return found;
}
}
type = type.GetTypeInfo().BaseType;
}
return null;
}