关于泛型的问题 [英] Question about Generics

查看:60
本文介绍了关于泛型的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想知道是否可以使用Generics执行以下操作:


让'' s说我有一个泛型成员变量作为泛型类的一部分

像这样:


列表< DLinqQuery< TDataContext>> _queries;


其中DLinqQuery是一个带类型参数的泛型类

TDataContext。


这个成员变量是一个名为
的泛型类的一部分
DLinqDataSource< TDataContext>并且作为指定类型的公共财产公开。


假设所有TDataContext类型都来自

具体类DataContext。


现在,让我们说我从DataContext派生了一个名为MyDataContext的类。


我创建了两个这样的具体类:


公共类MyDataQuery:DLinqQuery< MyDataContext> {}

公共类MyDataSource:DLinqDataSource< MyDataContext> {}


我希望能够覆盖MyDataSource类中的属性,以便

返回List< MyDataQuery>而不是通用的

列表< DLinqQuery< TDataContext>>这将成为

列表< DLinqQuery< MyDataContext>在MyDataSource的实例化中。


不幸的是,将基本属性值(这是一个

List< DLinqQuery< MyDataContext>>)转换为类型List< MyDataQuery>只是

无法正常工作,但从功能的角度来看,它似乎是
会。


任意关于是否可以这样做的想法?

Hello,

I''m wondering if it''s possible to do the following with Generics:

Let''s say I have a generic member variable as part of a generic class
like this:

List<DLinqQuery<TDataContext>> _queries;

where DLinqQuery is a generic class that takes a type parameter
TDataContext.

This member variable is part of a generic class called
DLinqDataSource<TDataContext> and is exposed as a public property of
the specified type.

It is assumed that all TDataContext types will be derived from the
concrete class DataContext.

Now, let''s say I derive a class from DataContext called MyDataContext.

I the create two concrete classes like this:

public class MyDataQuery : DLinqQuery<MyDataContext>{}
public class MyDataSource : DLinqDataSource<MyDataContext>{}

I want to be able to override the property in the MyDataSource class to
return a List<MyDataQuery> instead of the generic
List<DLinqQuery<TDataContext>> which would become
List<DLinqQuery<MyDataContext> in an instantiation of MyDataSource.

Unfortunately, casting the base property value (which is a
List<DLinqQuery<MyDataContext>> to the type List<MyDataQuery> just
doesn''t work, but from a functional perspective it seems like that it
would.

Any ideas on whether this can be done?

推荐答案

你不能这样做的原因是因为通用类型不是

covariant(我相信这是一个术语)。这意味着如果你有一个类型

Dog来自Animal,而一个List< Dog>,你就不能把它变成一个

列表<动物> ;.


这样做的原因是,当你有一个类时,Cat从动物中获得
,你有新的List< Animal>(从List< Dog>中投射),

列表在添加它时不知道如何处理它。


工作原理这里是定义你的类:


公共类MyDataQuery:DLinqQuery< MyDataContext> {}


公共类MyDataSource< T> :DLinqDataSource< T> {}

然后,当您声明MyDataSource时,您会这样做:


MyDataSource< MyDataQuery> ds = new MyDataSource< MyDataQuery>();


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" ha **************** @ gmail.com" <公顷********* @ gmail.com>在消息中写道

news:11 ********************** @ y41g2000cwy.googlegr oups.com ...
The reason you can not do this is because generic types are not
covariant (I believe that is the term). This means that if you have a type
Dog which derives from Animal, and a List<Dog>, you can not cast it to a
List<Animal>.

The reason for this is that when you have a class, Cat which derives
from Animal, and you have the new List<Animal> (cast from List<Dog>), that
list wouldn''t know what to do with the Cat when it is added.

The workaround here is to define your classes like so:

public class MyDataQuery : DLinqQuery<MyDataContext>{}

public class MyDataSource<T> : DLinqDataSource<T>{}

Then, when you declare your MyDataSource, you would do this:

MyDataSource<MyDataQuery> ds = new MyDataSource<MyDataQuery>();

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"ha****************@gmail.com" <ha*********@gmail.com> wrote in message
news:11**********************@y41g2000cwy.googlegr oups.com...
您好,

我想知道是否可以使用Generics执行以下操作:

让我们说我有一个通用成员变量作为泛型类的一部分
这样:

List< DLinqQuery< TDataContext>> _queries;

其中DLinqQuery是一个采用类型参数的通用类。
TDataContext。

这个成员变量是名为
DLinqDataSource<的泛型类的一部分; TDataContext>并且作为指定类型的公共属性公开。

假设所有TDataContext类型都将从
具体类DataContext派生。

我创建了两个具体的类:

公共类MyDataQuery:DLinqQuery< MyDataContext> ; {}
公共类MyDataSource:DLinqDataSource< MyDataContext> {}
我希望能够覆盖MyDataSource类中的属性以返回List< MyDataQuery>而不是通用的列表< DLinqQuery< TDataContext>>这将成为列表< DLinqQuery< MyDataContext>在MyDataSource的实例化中。不幸的是,将基本属性值(这是一个列表< DLinqQuery< MyDataContext>>)转换为类型List< MyDataQuery>只是
不起作用,但从功能的角度来看,它似乎会这样。

关于是否可以这样做的任何想法?
Hello,

I''m wondering if it''s possible to do the following with Generics:

Let''s say I have a generic member variable as part of a generic class
like this:

List<DLinqQuery<TDataContext>> _queries;

where DLinqQuery is a generic class that takes a type parameter
TDataContext.

This member variable is part of a generic class called
DLinqDataSource<TDataContext> and is exposed as a public property of
the specified type.

It is assumed that all TDataContext types will be derived from the
concrete class DataContext.

Now, let''s say I derive a class from DataContext called MyDataContext.

I the create two concrete classes like this:

public class MyDataQuery : DLinqQuery<MyDataContext>{}
public class MyDataSource : DLinqDataSource<MyDataContext>{}

I want to be able to override the property in the MyDataSource class to
return a List<MyDataQuery> instead of the generic
List<DLinqQuery<TDataContext>> which would become
List<DLinqQuery<MyDataContext> in an instantiation of MyDataSource.

Unfortunately, casting the base property value (which is a
List<DLinqQuery<MyDataContext>> to the type List<MyDataQuery> just
doesn''t work, but from a functional perspective it seems like that it
would.

Any ideas on whether this can be done?



ha ************ **** @gmail.com 写道:
ha****************@gmail.com wrote:
你好,

我想知道是否可以这样做跟随泛型:

让我们说我有一个泛型成员变量作为泛型类的一部分
这样:

List< DLinqQuery< TDataContext> > _queries;

其中DLinqQuery是一个带类型参数的泛型类。
TDataContext。

这个成员变量是一个名为
DLinqDataSource< TDataContext>并公开作为指定类型的公共属性。

假设所有TDataContext类型都来自
具体类DataContext。

现在,让我们说我从DataContext派生了一个名为MyDataContext的类。

我创建了两个具体的类:

公共类MyDataQuery:DLinqQuery< MyDataContext> {}
公共类MyDataSource:DLinqDataSource< MyDataContext> {}
我希望能够覆盖MyDataSource类中的属性以返回List< MyDataQuery>而不是通用的列表< DLinqQuery< TDataContext>>这将成为列表< DLinqQuery< MyDataContext>在MyDataSource的实例化中。不幸的是,将基本属性值(这是一个列表< DLinqQuery< MyDataContext>>)转换为类型List< MyDataQuery>只是
不起作用,但从功能的角度来看,似乎就是这样。

关于是否可以这样做的任何想法?
Hello,

I''m wondering if it''s possible to do the following with Generics:

Let''s say I have a generic member variable as part of a generic class
like this:

List<DLinqQuery<TDataContext>> _queries;

where DLinqQuery is a generic class that takes a type parameter
TDataContext.

This member variable is part of a generic class called
DLinqDataSource<TDataContext> and is exposed as a public property of
the specified type.

It is assumed that all TDataContext types will be derived from the
concrete class DataContext.

Now, let''s say I derive a class from DataContext called MyDataContext.

I the create two concrete classes like this:

public class MyDataQuery : DLinqQuery<MyDataContext>{}
public class MyDataSource : DLinqDataSource<MyDataContext>{}

I want to be able to override the property in the MyDataSource class to
return a List<MyDataQuery> instead of the generic
List<DLinqQuery<TDataContext>> which would become
List<DLinqQuery<MyDataContext> in an instantiation of MyDataSource.

Unfortunately, casting the base property value (which is a
List<DLinqQuery<MyDataContext>> to the type List<MyDataQuery> just
doesn''t work, but from a functional perspective it seems like that it
would.

Any ideas on whether this can be done?







这个问题在这里已被问过几次了。我给他们的一个给出的答案就是你不能把它投下来,因为List< A>不是,并且不是List< B>的衍生物,如果A派生自B.它们是两种不同的类型,

因为类型表示为类型名称的结果,而且它的类型是

参数。


所以,你必须写一个函数将

列表< DLinqQuery< MyDataContext>>转换为List< MyDataQuery> ;.另外,定义

的覆盖是改变be在
基类中定义的虚拟方法的haviour,不要改变它的签名,例如:


public virtual bool foo(int bar) {...}


只能覆盖以改变''foo''的行为,而不是它''返回

类型,而不是它的参数。


泛型List类包含一个名为ConvertAll<>的泛型方法。

可以帮助您转换列表,或者您可以通过列表枚举

并构建一个全新的。


-

希望这会有所帮助,

Tom Spink



Hi,

This question has been asked a few times here. The answer I gave to one of
them was that you cannot cast it, because a List<A> is not, and is not a
derivative of a List<B> if A derives from B. They''re two different types,
because the type is expressed as a result of the type name and it''s type
parameters.

So, you''ll have to write a function that will convert the
List<DLinqQuery<MyDataContext>> to List<MyDataQuery>. Also, the definition
of override is to change the behaviour of a virtual method defined in a
base class, not to alter it''s signature, e.g.:

public virtual bool foo ( int bar ) { ... }

Can only be overriden to change the behaviour of ''foo'', not it''s return
type, and not it''s parameters.

The generic List class contains a generic method called ConvertAll<> which
can help you convert the list, or you can just enumerate through your list
and build a brand new one.

--
Hope this helps,
Tom Spink


Nicholas Paldino [.NET / C#MVP]< ; mv*@spam.guard.caspershouse.comwrote:
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:

你不能这样做的原因是因为泛型类型不是

covariant (我相信这就是这个词)。
The reason you can not do this is because generic types are not
covariant (I believe that is the term).



它是 - 尽管CLR本身支持协方差。你可以告诉

CLR某些东西是从狗派生的某种类型的列表

或某种类型的超类型狗 ;。但是,C#并没有公开这个,而框架库也没有。


这有点遗憾 - Java库*做*暴露这个,并且

虽然这是一个相当令人困惑的话题,但它可以非常方便。有了Mads

现在在C#团队中,我不会感到惊讶看到

协方差/逆变出现在更高版本中 - 尽管现在是

标准库已被定义,没有支持,

它们的用处会大大减少。


(我应该说这是其中之一Java#

泛型优于C#泛型的几个地方。)


-

Jon Skeet - < sk ***@pobox.com>
http://www.pobox.com / ~siget 博客: http://www.msmvps.com/jon .skeet

如果回复小组,请不要给我发邮件

It is - although the CLR itself supports covariance. You can tell the
CLR that something is a list of "some type which is derived from Dog"
or "some type which is a super-type of Dog". However, C# doesn''t expose
this, and neither do the framework libraries.

It''s a bit of a shame - the Java libraries *do* expose this, and
although it''s a fairly confusing topic, it can be very handy. With Mads
on the C# team now, I wouldn''t be surprised to see
covariance/contravariance appear in a later version - although now the
standard libraries have been defined without support, the usefulness of
them would be very much diminished.

(I should say that this is one of the very few places where Java
generics are better than C# generics.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


这篇关于关于泛型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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