获取实现特定开放泛型类型的所有类型
如何获取实现特定开放泛型类型的所有类型?
例如:
public interface IUserRepository : IRepository<User>
查找所有实现 IRepository
的类型。
public static IEnumerable<Type> GetAllTypesImplementingOpenGenericType(Type openGenericType, Assembly assembly)
{
...
}
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)
{
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这将返回继承通用基类的所有类型。并非所有类型都继承通用接口。
这将返回所有类型,包括继承链中具有开放泛型类型的接口、抽象和具体类型。
在此示例中,第二种方法将查找 ConcreteUserRepo 和 IUserRepository:
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:
不使用 LINQ 实现的解决方案,搜索泛型和非泛型接口,过滤类的返回类型。
Solution implemented without LINQ, searching both generic and non generic interfaces, filtering the return type to classes.
你可以尝试
或者
You could try
or
您可以使用以下代码获取所有实现
IRepository
接口的类型:You can use the following code to get all types that implement
IRepository<>
interface: