无法理解如何使用泛型来做到这一点 [英] can't understand how to do this with generics

查看:75
本文介绍了无法理解如何使用泛型来做到这一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个通用类,例如:

公共类MyGroup< T> :Collection< MyChildClass>


,我想从其中一个方法中将自己的引用传递给另一个类中的

跟随方法:


public void OnValidateChanged(MyGroup< MyChildClass> group)


我正在尝试:


_myReferenceToTheOtherClass.OnValidateChanged(this);


错误信息是:


错误2参数''1':无法转换'' MyNamespace.MyGroup< T>''

''MyNamespace.MyGroup<''MyNamespace.MyChildClass>


我已经尝试了所有可能的想法但是,不能破解它。我想b $ b猜我还不完全明白这个< T>东西。


谢谢


巴里莫斯曼

Hi, if I have a generic class such as:
public class MyGroup<T> : Collection<MyChildClass>

and from one of it''s methods I want to pass a reference to myself to the
following method in another class:

public void OnValidateChanged(MyGroup<MyChildClass> group)

I am trying:

_myReferenceToTheOtherClass.OnValidateChanged(this );

the error message is:

Error 2 Argument ''1'': cannot convert from ''MyNamespace.MyGroup<T>'' to
''MyNamespace.MyGroup<''MyNamespace.MyChildClass>

I have tried all the possibilities I can think of, but can''t crack it. I
guess I don''t fully understand this <T> stuff.

thanks

Barry Mossman

推荐答案

Barry Mossman < NO **** @ NoSpam.com> écritdansle message de news:
eT ******* *******@TK2MSFTNGP02.phx.gbl ...


|如果我有一个通用类,如:

|公共类MyGroup< T> :Collection< MyChildClass>

|

|从其中一个方法我想将自己的引用传递给

|以下方法在另一个班级:

|

| public void OnValidateChanged(MyGroup< MyChildClass> group)

|

|我正在尝试:

|

| _myReferenceToTheOtherClass.OnValidateChanged(this);

|

|错误信息是:

|

|错误2参数''1':无法从''MyNamespace.MyGroup< T>''转换为

| ''MyNamespace.MyGroup<''MyNamespace.MyChildClass>

|

|我已经尝试了所有可以想到的可能性,但是不能破解它。我

|猜我不完全明白这个< T>东西。


< T>期望在MyGroup中绑定特定类型。除非你将
绑定到MyChildClass,否则你不能传递this。作为MyGroup< MyChaildClass>

除非您将T约束为MyChildClass。


公共类MyGroup< T> :Collection< MyChildClass>其中T:MyChildClass


Joanna


-

Joanna Carter [TeamB]

顾问软件工程师
"Barry Mossman" <NO****@NoSpam.com> a écrit dans le message de news:
eT**************@TK2MSFTNGP02.phx.gbl...

| Hi, if I have a generic class such as:
| public class MyGroup<T> : Collection<MyChildClass>
|
| and from one of it''s methods I want to pass a reference to myself to the
| following method in another class:
|
| public void OnValidateChanged(MyGroup<MyChildClass> group)
|
| I am trying:
|
| _myReferenceToTheOtherClass.OnValidateChanged(this );
|
| the error message is:
|
| Error 2 Argument ''1'': cannot convert from ''MyNamespace.MyGroup<T>'' to
| ''MyNamespace.MyGroup<''MyNamespace.MyChildClass>
|
| I have tried all the possibilities I can think of, but can''t crack it. I
| guess I don''t fully understand this <T> stuff.

The <T> in MyGroup is expecting to be bound to a particular type. Unless you
bind it to MyChildClass, you cannot pass "this" as MyGroup<MyChaildClass>
unless you constrain the T to MyChildClass.

public class MyGroup<T> : Collection<MyChildClass> where T : MyChildClass

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


Joanna Carter [TeamB] ha scritto:
Joanna Carter [TeamB] ha scritto:
public class MyGroup< T> :Collection< MyChildClass>其中T:MyChildClass
public class MyGroup<T> : Collection<MyChildClass> where T : MyChildClass




我很新在C#(和泛型)但是...

....其中'与以下区别:


公共类MyGroup< MyChildClass> :Collection< MyChildClass>


谢谢,

Giulio - Italia



I''m quite "new" in C# (and in generics) but...
....where''s the difference from:

public class MyGroup<MyChildClass> : Collection<MyChildClass>

Thanks,
Giulio - Italia


" Joanna Carter [ TeamB] QUOT; <乔**** @ not.for.spam>在消息中写道

news:Om ************** @ TK2MSFTNGP02.phx.gbl ...
"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:Om**************@TK2MSFTNGP02.phx.gbl...
< T>期望在MyGroup中绑定特定类型。除非你将它绑定到MyChildClass,否则你不能传递this。作为MyGroup< MyChaildClass>
除非您将T约束为MyChildClass。

公共类MyGroup< T> :Collection< MyChildClass>其中T:MyChildClass
Joanna Carter [TeamB]
顾问软件工程师
The <T> in MyGroup is expecting to be bound to a particular type. Unless
you
bind it to MyChildClass, you cannot pass "this" as MyGroup<MyChaildClass>
unless you constrain the T to MyChildClass.

public class MyGroup<T> : Collection<MyChildClass> where T : MyChildClass
Joanna Carter [TeamB]
Consultant Software Engineer




感谢Joanna,很高兴看到TeamB也在这里运营< g>


我添加了where约束,但仍然从

后续行获取错误消息,例如:

MyGroup< MyChildClass> xxx = this;


参数''1':不能隐式地将类型''MyNamespace.MyGroup< T>''转换为

''MyNamespace。 MyGroup<''MyNamespace.MyChildClass>


您有任何进一步的见解吗?


谢谢


Barry Mossman



Thanks Joanna, it is good to see TeamB operating over here as well <g>

I added the where constraint, but still get the error message from the
following line for example:
MyGroup<MyChildClass> xxx = this;

Argument ''1'': cannot implicitly convert type ''MyNamespace.MyGroup<T>'' to
''MyNamespace.MyGroup<''MyNamespace.MyChildClass>

Have you any further insight?

thanks

Barry Mossman


这篇关于无法理解如何使用泛型来做到这一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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