You cannot cast a Generic<Derived>
to a Generic<Base>
.
Just imagine if you could. You have a List<Wolf>
and cast it to a List<Animal>
. Now you could .Add()
a Sheep
to your List<Animal>
. But wait... now your List<Wolf>
contains a Sheep
. What a mess.
This would only work out if you could make sure that the thing you cast to is read-only in all possible forms. This is was co- and contravariance is all about. It only works for interfaces though.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…