Generic derived from Generic

P

phancey

if I have a Generic class i.e.

ObjectManager<T> where T : myBaseObject

and a derived class i.e
CustomerManager : ObjectManager<Customer>

How can I then write a generic class like:
MyUIPage<T> where T : ObjectManager

so that I can then create classes like
MyCustomerPage<CustomerManager>?

I can't do the MyUIPage<T> base class like that because ObjectManager
has to have types too but I don't want to be caring about those types
when deriving MyCustomerPage. I just want to specify CustomerManager.
Then when I call methods of my manager it will return the relevant
type.

It seems however that I have to do MyUIPage<T,E> where
T:ObjectManager<E> and then MyCustomerPage<CustomerManager,Customer>
which seems pointless. If I have specified CustomerManager why do I
need to specify Customer again - that is already in the definition of
CustomerManager.

Any help appreciated

thanks
Phil
 
P

Pavel Minaev

if I have a Generic class i.e.

ObjectManager<T> where T : myBaseObject

and a derived class i.e
CustomerManager : ObjectManager<Customer>

How can I then write a generic class like:
MyUIPage<T> where T : ObjectManager

so that I can then create classes like
MyCustomerPage<CustomerManager>?

I can't do the MyUIPage<T> base class like that because ObjectManager
has to have types too but I don't want to be caring about those types
when deriving MyCustomerPage. I just want to specify CustomerManager.
Then when I call methods of my manager it will return the relevant
type.

It seems however that I have to do MyUIPage<T,E> where
T:ObjectManager<E> and then MyCustomerPage<CustomerManager,Customer>
which seems pointless. If I have specified CustomerManager why do I
need to specify Customer again - that is already in the definition of
CustomerManager.

This isn't pointless per se, since you technically do have two type
parameters for your MyUIPage class. What you really want is for it to
be able to deduce E from T, so that at the point of use, you only need
to specify T. Unfortunately, C# does not do that (yet, anyway), so
your solution is the only one you can use. There isn't anything
analogous to C++ template template arguments to solve it otherwise.
 
P

phancey

This isn't pointless per se, since you technically do have two type
parameters for your MyUIPage class. What you really want is for it to
be able to deduce E from T, so that at the point of use, you only need
to specify T. Unfortunately, C# does not do that (yet, anyway), so
your solution is the only one you can use. There isn't anything
analogous to C++ template template arguments to solve it otherwise.- Hidequoted text -

- Show quoted text -

ok - thanks for your time and help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top