【发布时间】:2016-08-19 13:57:45
【问题描述】:
我有一个通用控制器,我将一个只包含属性的类传递给它。这一切都很好,但是......
我想在Controller类中创建另一个类来继承传递的类。
有点像这样:
public class Products
{
public Int32 ProductID {get; set;}
public String ProductName {get; set;}
}
public class ProductController : Controller<Products>
{
public ProductsController() : base("Products", "ProductID", "table", "dbo")
{
}
}
public class Controller<T> : IDisposable where T : new()
{
protected Controller(String tablename, String keyname, String entitytype, String tableschema = "dbo")
{
...
}
//How do I create the Recordset class inheriting T
public class Recordset : T //<----This is what I don't know how to do
{
public Int32 myprop {get; set;}
public void MoveNext()
{
//do stuff
}
}
}
如何使用继承的 T 创建类 Recordset?
【问题讨论】:
-
您是否尝试过包括
where T : class, new()?你不能继承结构。 -
@Chris:看看 Nuget 上的
ImpromptuInterface,它可以动态创建新类型,就像其他类型一样;但你真的不想去那里。另见en.wikipedia.org/wiki/Composition_over_inheritance -
谢谢,贝林。就是这样。以为我以前做过,但是...下面的解决方案。
标签: c# class generics inheritance