为什么我不能从列表中转换<MyClass>到列表&lt; object&gt;? [英] Why cant I cast from a list&lt;MyClass&gt; to List&lt;object&gt;?

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

问题描述

我有一个对象列表,它是我的类型 QuoteHeader ,我想将此列表作为对象的列表传递到一个方法,它能够接受列表< object>



我的代码行为...

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

但我在设计时遇到以下错误...

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

我需要对我的班级做任何事情以允许这样吗?我认为所有的类继承自对象,所以不能理解为什么这不工作?

解决方案

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

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

但是animals长颈鹿名单;你不能把老虎放在长颈鹿列表中。



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

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

在C#4中,这是合法的在 IEnumerable ,而不是 IList

 列表< Giraffe> giraffes = new List< Giraffe>(); 
IEnumerable< Animal>动物=长颈鹿; //法律在C#4
foreach(动物中的动物){} //每个长颈鹿是一个动物,所以这是安全的

$这是安全的,因为 IEnumerable< T> 不公开接受 T的任何方法。

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




  • 创建一个新的对象列表

  • 使方法接受对象[]而不是 List< object> ,并使用不安全的数组协方差

  • 使方法通用,因此需要列表< T>

  • 该方法接受IEnumerable

  • 使方法接受 IEnumerable< object> 并使用C#4。


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 class inherit from object so cant understand why this wouldnt 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.

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

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