为什么我不能从 List中进行转换?列出<对象>?对象>
我有一个对象列表,其类型为 QuoteHeader
,我想将此列表作为对象列表传递给能够接受 List
我的代码行是...
Tools.MyMethod((List<object>)MyListOfQuoteHeaders);
但是我在设计时收到以下错误...
Cannot convert type 'System.Collections.Generic.List<MyNameSpace.QuoteHeader>'
to 'System.Collections.Generic.List<object>'
我需要对我的类执行任何操作才能允许此操作吗?我认为所有类都继承自对象,所以我不明白为什么这不起作用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这不合法的原因是它不安全。假设它是合法的:
但“动物”实际上是长颈鹿的列表;你不能把老虎列入长颈鹿名单。
不幸的是,在 C# 中,这对于引用类型的数组是合法的:
在 C# 4 中,这在 IEnumerable 上合法,但在 IList 上不合法:
它是安全的,因为
IEnumerable;
不会公开任何接受 T 的方法。要解决您的问题,您可以:
List,并使用不安全的数组协方差。
List
IEnumerable并使用 C# 4。
The reason this is not legal is because it is not safe. Suppose it were legal:
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:
In C# 4 this is legal on IEnumerable but not IList:
It is safe because
IEnumerable<T>
does not expose any method that takes in a T.To solve your problem you can:
List<object>
, and use unsafe array covariance.List<T>
IEnumerable<object>
and use C# 4.您无法将
List
转换为List
,因为它实际上是您要转换的列表的实例以及列表本身。有一个扩展方法可以让您执行此操作(MSDN 参考):
此方法将尝试将一种类型列表的每个实例转换为另一种类型,并将它们添加到新的枚举中。如果任何实例无法转换,它将抛出异常。
就系统而言,列表的两种类型只是不同的类型,所以这就像是在说:
为了能够进行强制转换,必须在可用类型之间进行隐式或显式转换,而这是没有的。 t(除非您提供了一个,您也许可以做到)。
你期望它能够工作,因为
List总是能够保存另一个列表中的任何类型,但是当你用这些术语思考它时,你就会明白为什么它不能。
我确信有一个技术上更胜任的答案,但这就是我认为的要点。
您可能有兴趣阅读 Eric Lippert 的协变和逆变系列,因为这可能对您有帮助。
这个问题也可能有用
You can't cast
List<OneType>
toList<OtherType>
as it is actually the instances of the list you want to cast, as well as the List itself.there is an extension method which will allow you to do this (MSDN reference):
This method will attempt to cast each instance of the list of one type to the other type and add them to a new enumerable. it will throw an exception if any instance can't be cast.
As far as the system is concerned the two types for your lists are just different types, so it is like saying:
To be able to do the cast there would have to be an implicit or explicit conversion between the types available, which there isn't (unless you provided one, which you might be able to do).
you expect it to work because
List<object>
will always be able to hold any type in another list, but when you think about it in those terms you can see why it doesn't.I'm sure there is a more technically competent answer, but that is the gist of it I think.
you might be interested in reading Eric Lippert's series on Covariance and Contravariance as this may be helpful to you.
This question may also be useful
我假设您的意思是列表的类型是相互继承的,或者可以从一种类型转换为另一种类型 - 在这种情况下,请尝试以下操作:
I'm presuming that you mean that the lists are of types which inherit from each other or can otherwise be cast from one type to another - in that case, try this:
感谢您的多次回复。
我将解释我想做的事情以及我提出的解决方案。
我需要一个方法,可以通过传入任意类型的对象列表来调用该方法,然后将该列表输出到 XML。传递给该方法的还有一个字符串,它是系统文件结构路径位置,指向 XML 文件将保存到的位置。由于我的类和类型数量不断增加,我想避免编写多个方法来满足每种类型的类。我不确定我是否以正确的方式解决了这个问题,但这是解决我的问题的一个轻量级解决方案并且有效。如果有任何问题,或者如果有人有任何意见,请随时...
所以...我的方法现在看起来像这样......
我的调用行显示...
就像我说的,它可能不是最简洁的解决方案,但它在我的场景中效果很好。
Thanks for the many responses.
I'll explain what I wanted to do and what I've come up with as a solution.
I needed a method that I could call by passing in a List of objects of any type and then output that list to XML. Also passed to the method would be a string which would be a system file structure path location which points to the location the XML file would be saved to. As I have an ever growing number of classes and types, I wanted to avoid writing multiple methods to cater for each type of class. I'm not sure if I've even gone about this the right way, but it's a lightweight solution to my problem and works. If there are any issues with it, or if anyone has any comments please feel free...
So... my method now looks like this...
... and my calling line reads...
Like I said, it may not be the tidiest solution, but it works well in my scenario.
问题在于,在编译时,编译器会发出 2 个单独的类,其中 1 个表示
List
,另一个表示List。它们本质上是两种不同的类型。这就是泛型类型的工作原理,至少在 .Net 中是这样。
假设这是.Net,你可以
这样做基本上
The problem is that at Compile time, the compiler emits 2 separate classes, 1 that represents
List<MyClass>
and one that representsList<Object>
. They are essentially 2 separate types. That's how Generic types work, in .Net at least.Assuming this is .Net, you could do
which basically does