Create an inherited class from a type parameter

I have a general controller to which I pass a class containing only properties. It all works great, but ...

I want to create another class in the Controller class that inherits the passed class.

Example:

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
        }
    }
}

How to create a Recordset class using T as inherited?

+4
source share
3 answers

The compiler will not let you do this (since I'm sure the error message was told to you):

"",
. , , .

:

public class Controller<T> : IDisposable where T : new()
{
    public class RecordSet 
    {    
        private T Records;

        public RecordSet(T records)
        {
            Records = records;
        }        

        public void MoveNext()
        {
            // pass through to encapsulated instance
            Records.MoveNext();
        }            
    }
}
+6

, Reflection.Emit. , , , , .

0
public class Controller<T> : IDisposable where T : class, new()
{
    protected Controller(String tablename, String keyname, String entitytype, String tableschema = "dbo")
    {
        ...
    }
   public class Recordset<TT> where TT : class, new()   
    {
        public TT myinheritedclass {get; set}
        public Int32 myprop {get; set;}

        public void MoveNext()
        {
            //do stuff
        }
    }

    public Recordset<T> myRecordset = new Recordset<T>()
}
0
source

All Articles