【发布时间】:2014-03-14 12:54:12
【问题描述】:
只知道泛型基类的类型,如何找到泛型基类的派生类?
我尝试了以下方法,但没有编译:
private Type FindRepositoryType(Type entityType)
{
var repoTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.BaseType != null
&& t.BaseType.IsGenericType
&& t.BaseType.GetGenericTypeDefinition() == typeof(BaseRepository<entityType>))
.ToList();
return null;
}
var repoType = FindRepositoryType(typeof(Product));
我希望找到 ProductRepository 类型:
public class ProductRepository : BaseRepository<Product>
{
}
【问题讨论】:
-
你想使用基类获取基类的派生类,是吗?
-
不,使用基类泛型类型,T。
标签: c# .net generics reflection types