My question has to do with casting classes inside a generic type. While I realize that casting objects like List<string>
to List<object>
requires support for covariance to prevent adding object
objects to a list that contains strings, I am wondering why a cast like below is not accepted by the compiler and whether it is solvable employing interfaces and co- or contravariance:
public class TypeA<T> {}
public class TypeB<T> {}
public class TypeC : TypeB<int> {}
class Program
{
public static void MyMethod<OutputType>(TypeA<TypeB<OutputType>> Parameter) {}
static void Main(string[] args)
{
TypeA<TypeC> Test = new TypeA<TypeC>();
MyMethod<int>(Test);
}
}
Compiling this results in an error:
Argument 1: cannot convert from 'ConsoleApplication1.TypeA<ConsoleApplication1.TypeC>' to 'ConsoleApplication1.TypeA<ConsoleApplication1.TypeB<int>>'
.
even though TypeC
is a direct descendent of TypeB<int>