在通用集合之间转换

时间:2012-11-12 23:15:02

标签: c# generics collections

在下面的示例中,为什么我不能将collectionA转换为collectionB,因为编译器知道TItemA<T>

public class A<T>
{
}

public void Foo<TItem, T> () where TItem : A<T>
{
    var collectionA = new List<TItem>();
    var collectionB = (List<A<T>>)collectionA; // "Cannot cast" error here
}

3 个答案:

答案 0 :(得分:1)

问题在于它允许您将不适当的项目放入collectionA。

这是对它的简化改造,希望能够更容易地看到问题:

假设你有(伪代码):

class Animal {...}

class Dog: Animal { Bark(){} }

class Cat: Animal { Meow(){} }

现在想象你可以这样做:

var dogs = new List<Dog>();

dogs.Add(new Dog());

dogs[0].Bark();

var animals = (List<Animal>) dogs;

然后你就可以做到这一点:

animals.Add(new Animal()); // Adds an Animal to the list 'dogs', which 'animals' references.

dogs[1].Bark(); // dogs will now have two elements, but the second isn't a dog -
                // so calling Bark() will explode.

答案 1 :(得分:0)

我认为这是因为您指示系统从List<X>转换为List<Y>,而不是说您要将列表中的每个项目从X转换为{{1} }}

你可以这样做:

Y

答案 2 :(得分:0)

您想要使用

var collectionB = collectionA.OfType<List<A<T>>>(); 

var collectionB = collectionA.Cast<List<A<T>>>();

第一个会忽略任何不属于List<A<T>>类型的东西,不能被视为它。如果列表中的某些内容无法转换,则第二个将抛出异常。

相关问题