相关疑难解决方法(0)

获取实现特定开放泛型类型的所有类型

如何获得实现特定开放泛型类型的所有类型?

例如:

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)

c# generics reflection open-generics

46
推荐指数
4
解决办法
2万
查看次数

获取类实现的泛型接口的类型参数

我有一个通用接口,比如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接口.

相关:确定类型是否实现了通用接口

c# generics reflection

31
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×2

generics ×2

reflection ×2

open-generics ×1