如何使用这个通用? [英] How to use this generic?

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

问题描述

我有这门课:


公共课MyClass< TValue>其中TValue:IMyClass,new()

{...}


我需要创建一个类来保存上面类的实例

使用List<>:


public class MyClassCollection:List< View< TValue>>

{...}


上面会出现这个错误:


无法找到类型或命名空间名称''TValue''(你错过了吗?) >
a使用指令或汇编参考?)


我该怎么做?


谢谢,

Brett

解决方案

Brett,


如果你没有指定TValue类声明,然后它假定

TValue是类型的名称,而不是类型参数。你想

做:


公共类MyClassCollection< TValue> :列表<查看< TValue>>

{}


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse。 com


" Brett Romero" <交流***** @ cygen.com>在消息中写道

news:11 ********************** @ y41g2000cwy.googlegr oups.com ...

我有这个课程:

公共课MyClass< TValue>其中TValue:IMyClass,new()
{...}
我需要使用List<>创建一个包含上述类的实例的类:<公共类MyClassCollection:列表<查看< TValue>>
{...}

上面会出现此错误:
找不到类型或命名空间名称''TValue''(您是否缺少
使用指令或程序集引用?)

我该怎么做?

谢谢,
Brett





Nicholas Paldino [.NET / C#MVP]写道:< blockquote class =post_quotes> Brett,

如果你没有在类声明中指定TValue,那么它假定TValue是类型的名称,而不是类型参数。你想做什么:

公共类MyClassCollection< TValue> :列表<查看< TValue>>
{}

希望这有帮助。




这样做的问题意味着该集合只会持有一种MyClass的
。让我们说我这样做:


MyClass< green> green = new MyClass< green>();

MyClass< red> red = new MyClass< red>();

MyClass< orange> orange = new MyClass< orange>();


现在我想将它们中的每一个添加到MyClassCollection中。使用你的

技术,它看起来像这样:

MyClassCollection< green> ColorCollection = new

MyClassCollection< green>();


我不能做MyClassCollection.Add(红色)因为它只需要类型的

绿色。我希望这个系列能够容纳所有三种颜色。


谢谢,

Brett


< blockquote> Brett,


然后你需要找到他们共享的基类/接口,并且

使用最小的公分母。


所以说绿色,红色和橙色来自Color。你的课程看起来像

喜欢:


公共类MyClassCollection:列表<查看<颜色>>

{}


然后你必须使用红色,绿色和橙色来获得

特定类型的功能。


- -

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam.guard.caspershouse.com


" Brett Romero" <交流***** @ cygen.com>在消息中写道

news:11 ********************** @ i40g2000cwc.googlegr oups.com ...


Nicholas Paldino [.NET / C#MVP]写道:

Brett,

如果你没有在类声明中指定TValue,那么它
假定TValue是类型的名称,而不是类型参数。你想要
来做:公共类MyClassCollection< TValue> :列表<查看< TValue>>
{}

希望这有帮助。



这样做的问题意味着收集将只持有一种MyClass。让我们说我这样做:

MyClass< green> green = new MyClass< green>();
MyClass< red> red = new MyClass< red>();
MyClass< orange> orange = new MyClass< orange>();

现在我想将它们中的每一个添加到MyClassCollection中。使用您的
技术,它看起来像这样:

MyClassCollection< green> ColorCollection = new
MyClassCollection< green>();

我不能做MyClassCollection.Add(红色),因为它只需要绿色类型。我希望这个系列能够容纳所有三种颜色。

谢谢,
Brett



I have this class:

public class MyClass<TValue> where TValue : IMyClass, new( )
{...}

I need to create a class that will hold instances of the above class
using List<>:

public class MyClassCollection : List<View<TValue>>
{...}

The above will give this error:

The type or namespace name ''TValue'' could not be found (are you missing
a using directive or an assembly reference?)

How can I do this?

Thanks,
Brett

解决方案

Brett,

If you do not specify TValue in the class declaration, then it assumes
that TValue is the name of the type, and not a type parameter. You want to
do:

public class MyClassCollection<TValue> : List<View<TValue>>
{}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@y41g2000cwy.googlegr oups.com...

I have this class:

public class MyClass<TValue> where TValue : IMyClass, new( )
{...}

I need to create a class that will hold instances of the above class
using List<>:

public class MyClassCollection : List<View<TValue>>
{...}

The above will give this error:

The type or namespace name ''TValue'' could not be found (are you missing
a using directive or an assembly reference?)

How can I do this?

Thanks,
Brett




Nicholas Paldino [.NET/C# MVP] wrote:

Brett,

If you do not specify TValue in the class declaration, then it assumes
that TValue is the name of the type, and not a type parameter. You want to
do:

public class MyClassCollection<TValue> : List<View<TValue>>
{}

Hope this helps.



The problem with doing it that way means the collection will only hold
one type of MyClass. Let''s say i do this:

MyClass<green> green = new MyClass<green>();
MyClass<red> red = new MyClass<red>();
MyClass<orange> orange = new MyClass<orange>();

Now I want to add each of them to the MyClassCollection. Using your
technique, it looks like this:

MyClassCollection<green> ColorCollection = new
MyClassCollection<green>();

I can''t do MyClassCollection.Add(red) because it only takes types of
green. I want this collection to hold all three colors.

Thanks,
Brett


Brett,

Then you need to find a base class/interface that they all share, and
work with the lowest common denominator.

So say Green, Red, and Orange derive from Color. Your class would look
like:

public class MyClassCollection : List<View<Color>>
{}

And then you would have to cast to Red, Green and Orange for the
specific type functionality.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...


Nicholas Paldino [.NET/C# MVP] wrote:

Brett,

If you do not specify TValue in the class declaration, then it
assumes
that TValue is the name of the type, and not a type parameter. You want
to
do:

public class MyClassCollection<TValue> : List<View<TValue>>
{}

Hope this helps.



The problem with doing it that way means the collection will only hold
one type of MyClass. Let''s say i do this:

MyClass<green> green = new MyClass<green>();
MyClass<red> red = new MyClass<red>();
MyClass<orange> orange = new MyClass<orange>();

Now I want to add each of them to the MyClassCollection. Using your
technique, it looks like this:

MyClassCollection<green> ColorCollection = new
MyClassCollection<green>();

I can''t do MyClassCollection.Add(red) because it only takes types of
green. I want this collection to hold all three colors.

Thanks,
Brett



这篇关于如何使用这个通用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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