List(Of clsClass)= List(Of clsSubClass) [英] List(Of clsClass) = List(Of clsSubClass)

查看:78
本文介绍了List(Of clsClass)= List(Of clsSubClass)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个继承自clsClass的类clsSubClass。


当我实例化clsClass(MyClass)的对象时,我从clsSubclass(MySubClass)实例化一个

对象我可以做一个MyClass = MySubclass。


但是当我声明它们的两个通用列表时(Dim MyList1 as List(of

clsClass)" andDim MyList2 as List(Of clsSubClass)),我不能做

MyList1 = MyList2。


为什么这是完全正确的,有没有办法实现这种行为?

非常感谢,

Pieter

Hi,

I have a class clsSubClass which inherits from clsClass.

When I instantiate an object of clsClass (MyClass), and I instantiate an
object from clsSubclass (MySubClass) I can do an "MyClass = MySubclass".

But when I declare two generic list of them ("Dim MyList1 as List(Of
clsClass)" and "Dim MyList2 as List(Of clsSubClass)"), I can''t do an
"MyList1 = MyList2".

Why is this exactly, and is there a way to implement this behaviour?
Thanks a lot in advance,
Pieter

推荐答案

" Pieter" < pi **************** @ hotmail.comwrote:
"Pieter" <pi****************@hotmail.comwrote:

当我实例化clsClass(MyClass)的对象时,我从clsSubclass(MySubClass)实例化一个

对象我可以做一个MyClass = MySubclass。

但是当我声明它们的两个通用列表时( Dim MyList1 as List(of

clsClass)"和Dim MyList2 as List(Of clsSubClass)"),我不能做一个

" MyList1 = MyList2"。

为什么这是确切的,有没有办法实现这种行为?
When I instantiate an object of clsClass (MyClass), and I instantiate an
object from clsSubclass (MySubClass) I can do an "MyClass = MySubclass".
But when I declare two generic list of them ("Dim MyList1 as List(Of
clsClass)" and "Dim MyList2 as List(Of clsSubClass)"), I can''t do an
"MyList1 = MyList2".
Why is this exactly, and is there a way to implement this behaviour?



你不能这样做因为它允许你将超类实例添加到

a列表中,根据它的定义,它只是应该包含子类

实例。


当然,你可以编写一个循环来将元素复制到单独的列表中

另一种类型。


Eq。

You can''t do that because it would allow you to add superclass instances to
a list that, by its definition, is only supposed to contain subclass
instances.

Of course, you can write a loop to copy the elements to a separate list of
another type.

Eq.


7月15日,9:34 * am,Pieter < pieterNOSPAMcou ... @ hotmail.comwrote:
On Jul 15, 9:34*am, "Pieter" <pieterNOSPAMcou...@hotmail.comwrote:

我有一个继承自clsClass的类clsSubClass。


我实例化一个clsClass(MyClass)的对象,并从clsSubclass(MySubClass)实例化一个

对象我可以做一个MyClass = MySubclass...


但是当我声明它们的两个通用列表(Dim MyList1 as List(Of

clsClass)和Dim MyList2 as List(Of clsSubClass))时,我可以'做一个

" MyList1 = MyList2"。
I have a class clsSubClass which inherits from clsClass.

When I instantiate an object of clsClass (MyClass), and I instantiate an
object from clsSubclass (MySubClass) I can do an "MyClass = MySubclass"..

But when I declare two generic list of them ("Dim MyList1 as List(Of
clsClass)" and "Dim MyList2 as List(Of clsSubClass)"), I can''t do an
"MyList1 = MyList2".



的确如此。这是因为泛型不会出现差异。

Indeed. That''s because generics don''t exhibit variance.


为什么这是完全正确的,有没有办法实现这种行为?
Why is this exactly, and is there a way to implement this behaviour?



简要回答:考虑这段代码。

List< BananabananaBunch = new List< Banana>();

列表< Fruitfruitbowl = bananaBunch;

fruitbowl.Add(新Apple());


假设这是合法的 - 那么bananaBunch将包含一个Apple,

显然是无效的。


有关详细信息,请参阅Eric Lippert的一系列博客文章:
http://blogs.msdn.com/ericlippert/ar ... e / default.aspx


Jon

Brief answer: consider this code.
List<BananabananaBunch = new List<Banana>();
List<Fruitfruitbowl = bananaBunch;
fruitbowl.Add(new Apple());

Suppose this were legal - then bananaBunch would contain an Apple,
which is clearly invalid.

For a lot of detail, see Eric Lippert''s series of blog articles:
http://blogs.msdn.com/ericlippert/ar...e/default.aspx

Jon


哦确实你是对的: - SI应该考虑到这一点:-)


虽然:导入也不起作用,这应该适用于我的操作吗?

因为clsSubClass是也是一个clsClass:它可以导入这些

项......

" Jon Skeet [C#MVP]" < sk *** @ pobox.com写信息

新闻:e6 *************************** ******* @ l64g2000 hse.googlegroups.com ...

7月15日上午9:34,Pieter < pieterNOSPAMcou ... @ hotmail.comwrote:
Oh yes indeed you''re right :-S I should have thought about that :-)

Although: An Import doesn''t work neither, which should work in my opnion?
Because clsSubClass is also an clsClass: it shoudl be able to import these
items...
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:e6**********************************@l64g2000 hse.googlegroups.com...
On Jul 15, 9:34 am, "Pieter" <pieterNOSPAMcou...@hotmail.comwrote:


这篇关于List(Of clsClass)= List(Of clsSubClass)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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