This is basically a design problem.
My class structure is as follows:
Interface IA
interface IB<T> where T : IA
abstract class AC<T> : IB<T>
class D : IA
class E : IA
class F : AC<D>
class G : AC<E>
Now, I have a method in AC whose signature is:
void foo(IB<IA> param)
And I want to call it from F like this:
foo(new G())
I know about the limitation of covariance and contravariance, and I know the language doesn't allow it. Also, I can't set IB<out T>
.
My question is, what is the correct way to handle such a situation design-wise?
It sounds like you need to make the Foo
method generic:
void Foo<T>(IB<T> param) where T : IA
At that point, Foo(new G())
would be implicitly Foo<E>(new G())
.
See more on this question at Stackoverflow