Passing a generic collection of objects to a method that requires a collection of the base type

烈酒焚心 提交于 2019-11-28 08:27:02

问题


Say I have a method that is expecting a generic collection parameter of a base type, see Test.MethodA(IEnumerable(BaseClass) listA) below. How come when I pass it a collection of a derived type the code wont build? Wouldn't all instances of DerivedClass also be a BaseClass?

I could have just created a new List(BaseClass) and passed that to MethodA(IEnumerable(BaseClass) listA). But I would think C# would be smart enough to know that a collection of DerivedClass has all the same properties as a collection of BaseClass.

Is using the List.Cast(T)() method as I've shown the best way to solve this problem?

abstract class BaseClass
{
    public int SomeField;
    public abstract string SomeAbstractField
    {
        get;
    }
}

class DerivedClass:BaseClass
{
    public override string SomeAbstractField
    {
        get { return "foo"; }
    }
}

class TestClass
{ 

    public void MethodA(IEnumerable<BaseClass> listA)
    {

    }

    public void MethodB()
    {
        List<DerivedClass> listB = new List<DerivedClass>();

        //Error 16  The best overloaded method match for 
        //TestClass.MethodA(List<BaseClass>)' 
        //has some invalid arguments
        this.MethodA(listB);

        //this works
        this.MethodA(listB.Cast<BaseClass>());
    }
}

回答1:


Cast<>() is the best way to solve it at the moment. Your original version would work fine in C# 4.0 / .NET 4.0 though, where IEnumerable<T> is covariant in T.

(I've just verified it compiles under .NET 4.0 beta 1.)

Until .NET 4.0 and C# 4 come out, generics are invariant - IEnumerable<object> and IEnumerable<string> are effectively unrelated interfaces. Even in .NET 4.0, you wouldn't be able to do this with List<T> as the parameter type - only interfaces and delegates will be variant, and even then only when the type parameter is only used in appropriate positions (output positions for covariance, input positions for contravariance).

To learn more about variance in C# 4, read Eric Lippert's excellent series of blog posts about it.




回答2:


In the general case (i.e. for collections that are modifiable), it is NOT the case that "a collection of DerivedClass has all the same properties as a collection of BaseClass": into a collection of Fruit you can insert Banana, Apple and Pear -- in a collection of Orange, you can't, so, you see, it doesn't have the same properties (under modification).

IEnumerable is a different case, since it's NOT modifiable, as as @Jon says this excessive restriction has been removed for that special case in 4.0.



来源:https://stackoverflow.com/questions/1174328/passing-a-generic-collection-of-objects-to-a-method-that-requires-a-collection-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!