为什么我无法从列表< MyClass>投射列出< object&gt ;? [英] Why can't I cast from a List<MyClass> to List<object>?

查看:114
本文介绍了为什么我无法从列表< MyClass>投射列出< object&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象列表,这些对象的类型是 QuoteHeader ,我想把这个列表作为一个对象列表传递给一个能够接受列表< object>



我的代码行显示...

  Tools.MyMethod((List< object>)MyListOfQuoteHeaders); 

但在设计时出现以下错误...

 无法将类型'System.Collections.Generic.List< MyNameSpace.QuoteHeader>'
转换为'System.Collections.Generic.List< object>'

我需要为我的课程做些什么才能做到这一点?我认为所有的类都从对象继承,所以我不明白为什么这不起作用?

解决方案

原因是不合法是因为它不安全。假设它是合法的:

  List< Giraffe>长颈鹿=新列表<长颈鹿>(); 
列表< Animal>动物=长颈鹿; //这是不合法的;假设它是。
animals.Add(new Tiger()); //将老虎放入动物列表总是合法的

但是动物实际上是长颈鹿名单;您不能将老虎列入长颈鹿列表中。



不幸的是,在C#中这与引用类型的数组合法:

 长颈鹿[]长颈鹿=新长颈鹿[10]; 
Animal []动物=长颈鹿; //合法!但是很危险,因为...
animals [0] = new Tiger(); // ...在运行时失败!

在C#4中,这对于 IEnumerable 是合法的,但不是IList :

 列表<长颈鹿>长颈鹿=新列表<长颈鹿>(); 
IEnumerable< Animal>动物=长颈鹿; //在C#中的合法性4
foreach(Animal animals in animals){} //每个长颈鹿都是动物,所以这是安全的

这是安全的,因为 IEnumerable< T> 不公开任何 / p>

要解决您的问题,您可以:


  • 创建一个新的对象列表。
  • 使方法获取一个对象[]而不是 List< object> ,并使用不安全的数组协方差。
  • 使方法为通用方法,因此它需要一个 List< T>

  • Make该方法采用IEnumerable

  • 使方法采用 IEnumerable< object> 并使用C#4。 / ul>

    I have a List of objects, which are of my type QuoteHeader and I want to pass this list as a list of objects to a method which is able to accept a List<object>.

    My line of code reads...

    Tools.MyMethod((List<object>)MyListOfQuoteHeaders);
    

    But I get the following error at design time...

    Cannot convert type 'System.Collections.Generic.List<MyNameSpace.QuoteHeader>' 
    to 'System.Collections.Generic.List<object>'
    

    Do I need to do anything to my class to allow this? I thought that all classes inherit from object so I can't understand why this wouldn't work?

    解决方案

    The reason this is not legal is because it is not safe. Suppose it were legal:

    List<Giraffe> giraffes = new List<Giraffe>();
    List<Animal> animals = giraffes; // this is not legal; suppose it were.
    animals.Add(new Tiger());  // it is always legal to put a tiger in a list of animals
    

    But "animals" is actually a list of giraffes; you can't put a tiger in a list of giraffes.

    In C# this is, unfortunately, legal with arrays of reference type:

    Giraffe[] giraffes = new Giraffe[10];
    Animal[] animals = giraffes; // legal! But dangerous because...
    animals[0] = new Tiger(); // ...this fails at runtime!
    

    In C# 4 this is legal on IEnumerable but not IList:

    List<Giraffe> giraffes = new List<Giraffe>();
    IEnumerable<Animal> animals = giraffes; // Legal in C# 4
    foreach(Animal animal in animals) { } // Every giraffe is an animal, so this is safe
    

    It is safe because IEnumerable<T> does not expose any method that takes in a T.

    To solve your problem you can:

    • Create a new list of objects out of the old list.
    • Make the method take an object[] rather than a List<object>, and use unsafe array covariance.
    • Make the method generic, so it takes a List<T>
    • Make the method take IEnumerable
    • Make the method take IEnumerable<object> and use C# 4.

    这篇关于为什么我无法从列表&lt; MyClass&gt;投射列出&lt; object&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆