Calling a method with an instance of derived class of derived generic type

Posted by madsbirk on Stack Overflow See other posts from Stack Overflow or by madsbirk
Published on 2013-10-27T21:51:33Z Indexed on 2013/10/27 21:53 UTC
Read the original article Hit count: 336

Filed under:
|
|

Okay, so I have classes

class B<T> : A<T>
class L : K

and a method

void Method(A<K> a) {...}

What I would like to do is this

b = new B<L>();
Method(b); //error

But it is not possible to b to the correct type. Indeed it is not possible to make this cast

A<K> t = new A<L>(); //error

I would really like to not have to change the internals of Method. I have no problems making changes to B and/or L. Do I have any options for making some sort of workaround? I guess it should be possible for Method to execute all of its method calls etc. on b, since B derives from A and L derives from K?

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics