列表与LT; T>遗产 [英] List<T> inheritance

查看:63
本文介绍了列表与LT; T>遗产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有一个经典问题:List< Tand List< Xwhere X是X类:

T.

好​​的,我知道问题:我如果X

是T,列表< Xis不是列表< T>,那么
我不知道是什么解决方案:)


让我们看看这个场景(显然没有编译):


// ---- ----------------------

class BaseClass

{}


class DerivedClass:BaseClass

{}


class BaseClassCollection:List< BaseClass>

{}


class DerivedClassCollection:List< DerivedClass>

{}


class Collector

{

BaseClassCollection _classes = new BaseClassCollection();

公共虚拟BaseClassCollection类

{

get {return _classes; }

}

}


类DerivedCollector:收藏家

{

public override DerivedClassCollection类

{

get {return _classes; }

}

}

// --------------------- -----


这就是我所拥有的:更基于类的更具体的类。

什么是更类似的解决方案? />

谢谢!

Hi all, I have a classic problem: List<Tand List<Xwhere X is a class X :
T.
Ok, I know the problem: I cannot cast List<Ton List<Xbecause also if X
is a T, List<Xis not a List<T>.
What I don''t know is the solution :)

Let''s see this scenario (that obviously doesn''t compile):

// --------------------------
class BaseClass
{ }

class DerivedClass : BaseClass
{ }

class BaseClassCollection : List<BaseClass>
{ }

class DerivedClassCollection : List<DerivedClass>
{ }

class Collector
{
BaseClassCollection _classes = new BaseClassCollection();
public virtual BaseClassCollection Classes
{
get { return _classes; }
}
}

class DerivedCollector : Collector
{
public override DerivedClassCollection Classes
{
get { return _classes; }
}
}
// --------------------------

This is what I would have: more specific classes that work on base classes.
What is the more similar solution?

Thanks!

推荐答案

通常这是通过通用方法解决的,例如

void SomeMethod(List< Tdata)其中T:SomeBase {


}


现在你可以传递List< SomeBaseor a List< SomethingElseas long

SomethingElse:SomeBase。请注意,在大多数情况下,你不需要在调用时指定

T,即


List< SomethingElsedata = //

SomeMethod(数据); //或者可以使用更长的SomeMethod< SomethingElse>(数据);


Marc
Typically this is solved with a generic method, e.g.

void SomeMethod(List<Tdata) where T : SomeBase {

}

Now you can pass either a List<SomeBaseor a List<SomethingElseas long as
SomethingElse : SomeBase. Note that in most cases you don''t need to specify
the T when calling - i.e.

List<SomethingElsedata = //
SomeMethod(data); // or could use longer SomeMethod<SomethingElse>(data);

Marc


不应该是:


void SomeMethod< T>(List< Tdata)其中T:SomeBase {

}





Christof


" Marc Gravell" < ma ********** @ gmail.comschrieb im Newsbeitrag

news:uf ************** @ TK2MSFTNGP04.phx.gbl ...
Shouldn''t it be:

void SomeMethod<T>(List<Tdata) where T: SomeBase {
}

?

Christof

"Marc Gravell" <ma**********@gmail.comschrieb im Newsbeitrag
news:uf**************@TK2MSFTNGP04.phx.gbl...

通常这是用通用方法解决的,例如


void SomeMethod(List< Tdata)其中T :SomeBase {


}


现在你可以传递List< SomeBaseor a List< SomethingElseas long

as SomethingElse:SomeBase。请注意,在大多数情况下,您不需要

在拨打时指定T - 即


List< SomethingElsedata = //

SomeMethod(数据); //或者可以使用更长的SomeMethod< SomethingElse>(数据);


Marc
Typically this is solved with a generic method, e.g.

void SomeMethod(List<Tdata) where T : SomeBase {

}

Now you can pass either a List<SomeBaseor a List<SomethingElseas long
as SomethingElse : SomeBase. Note that in most cases you don''t need to
specify the T when calling - i.e.

List<SomethingElsedata = //
SomeMethod(data); // or could use longer SomeMethod<SomethingElse>(data);

Marc



D''哦!是的 - 我错过了一个< T>,但在我的脑海中,我完全输入了它;-p

在记事本中编码和点击发送的危险......


他说的是什么


Marc
D''oh! yup ta - I missed a <T>, but in my head I typed it perfectly ;-p
The dangers of coding in notepad and hitting "Send"...

"what he said"

Marc


这篇关于列表与LT; T&GT;遗产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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