如何获得实现特定开放泛型类型的所有类型?
例如:
public interface IUserRepository : IRepository<User>
Run Code Online (Sandbox Code Playgroud)
找到所有实现的类型IRepository<>.
public static IEnumerable<Type> GetAllTypesImplementingOpenGenericType(Type openGenericType, Assembly assembly)
{
...
}
Run Code Online (Sandbox Code Playgroud) 我有一个通用接口,比如IGeneric.对于给定的类型,我想找到一个类通过IGeneric实现的泛型参数.
在这个例子中更清楚:
Class MyClass : IGeneric<Employee>, IGeneric<Company>, IDontWantThis<EvilType> { ... }
Type t = typeof(MyClass);
Type[] typeArgs = GetTypeArgsOfInterfacesOf(t);
// At this point, typeArgs must be equal to { typeof(Employee), typeof(Company) }
Run Code Online (Sandbox Code Playgroud)
GetTypeArgsOfInterfacesOf(Type t)的实现是什么?
注意:可以假设GetTypeArgsOfInterfacesOf方法是专门为IGeneric编写的.
编辑:请注意我特别询问如何从MyClass实现的所有接口中过滤掉IGeneric接口.