How do I get all types that implementing a specific open generic type?
For instance:
public interface IUserRepository : IRepository<User>
Find all types that implement IRepository<>
.
public static IEnumerable<Type> GetAllTypesImplementingOpenGenericType(Type openGenericType, Assembly assembly)
{
...
}
This will return all types that inherit a generic base class. Not all types that inherit a generic interface.
This will return all types, including interfaces, abstracts, and concrete types that have the open generic type in its inheritance chain.
This second method will find ConcreteUserRepo and IUserRepository in this example:
Solution implemented without LINQ, searching both generic and non generic interfaces, filtering the return type to classes.
You could try
or