I have a typed interface which I want to pass as a parameter. But the compiler (or the runtime if I add an explicit cast) doesn't like that.
Interface/Class definitions:
interface IModel { ... }
class ActualModelA : IModel { ... }
class ActualModelB : IModel { ... }
interface IView<T> where T : IModel { ... }
class ActualViewA : IView<ActualModelA> { ... }
class ActualViewB : IView<ActualModelB> { ... }
Here is the method where it shall be passed to:
static void Test(IView<IModel> view) { ... }
static void Main()
{
var view = new ActualViewA();
Test(view); // fails at compile time
Test((IView<IModel>) view); // compiles, but fails at runtime
}
the compile error is Argument 1: cannot convert from 'ActualViewA' to 'IView<IModel>'
which doesnt fully makes sense to me since ActualView
implements IView<ActualModelA>
and ActualModelA
implements IModel
.
What am I missing here?
The Test()
shall be able to receive an object of ActualViewA
and ActualViewB
.