IList to IList

后端 未结 8 1030
猫巷女王i
猫巷女王i 2021-01-05 00:28

I have a few classes:

class Vehicle
{
}

class Car : Vehicle
{
}

I have a list of the derived class: IList cars;

相关标签:
8条回答
  • 2021-01-05 00:46

    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.

    0 讨论(0)
  • 2021-01-05 00:46

    If you must use IList all of the way, then you are out of luck and the answers above can help you. However, if you can use an IList that is casted as IEnumerable and then simply re-casted at the destination as IList, that would work, since IEnumerable can accept such practice.

    // At the source or at the model.
    IEnumerable<BaseType> list = new List<Type>();
    // At the destination.
    IList<BaseType> castedList = (IList<BaseType>)list;
    

    Although, since the compiler cannot enforce these things, you must manually make sure that the types and base types indeed match, of course.

    0 讨论(0)
  • 2021-01-05 00:47

    Note that IReadOnlyList<T> from .NET 4.5+ will allow you to cast IReadOnlyList<Car> into IReadOnlyList<Vehicle> with no problems. List<T> and Collection<T> implement this interface.

    0 讨论(0)
  • 2021-01-05 00:51

    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>.

    0 讨论(0)
  • 2021-01-05 00:56
    var vehicles = cars.OfType<IVehicle>()
    
    0 讨论(0)
  • 2021-01-05 00:58

    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.

    0 讨论(0)
提交回复
热议问题