IList<Type> to IList<BaseType>
Use IEnumerable<T>
.Cast :
IList<Vehicle> vehicles = cars.Cast<Vehicle>().ToList();
Alternatively, you may be able to avoid the conversion to List depending on how you wish to process the source car list.
That sort of polymorphism that lets you cast IList<Car>
to IList<Vehicle>
is unsafe, because it would let you insert a Truck
in your IList<Car>
.
You're facing the problem that there is limited co- and contravariance in C#. There is an interesting approach in C# 4.0, described here at the very ending. However, it creates some other limitations that are related to the truck-problem in the answer from Novelocrat.