Re:投射通用IFoo< T> [英] Re: Cast generic IFoo<T>

查看:62
本文介绍了Re:投射通用IFoo< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不要将通用类型误认为是你想要的类型!!


IFoo<与IFoo没什么共同之处< B>!

它们是完全不同的类型,在运行时动态创建。


你问的有点类似于问:System.Web.UI和

System.Windows.Controls命名空间都包含一个Control类,我可以使用

代替另一个吗?通常他们有相同的名字!


如果你想使用两者共有的方法,你应该按照Alun的建议做。

继承IFoo< t :IFoo。


惊喜,惊讶,这正是他们在微软所做的,你会注意到,

例如,那个/>
IList< T:IList

IEnumerator< T:IEnumerator

ICollection< T:ICollection

等....

Don''t mistake generic type for what you would like them to be!!

IFoo<Ahas nothing in common with IFoo<B>!
They are completely different type create dynamically at runtime.

What you ask is a bit akin to ask: "the System.Web.UI and
System.Windows.Controls namespace both contains a Control class, could I use
one in place of the other? common they have the same name!"

If you want to use a method common to both you should do as Alun suggested.
Inherhit IFoo<t: IFoo.

Surprise, surprise, it''s exactly what they did at Microsoft, you''ll notice,
for exemple, that
IList<T: IList
IEnumerator<T: IEnumerator
ICollection<T: ICollection
etc....

推荐答案

7月24日,1:30 * am,Lloyd Dupont < some ... @ somewhere.netwrote:


< snip>
On Jul 24, 1:30*am, "Lloyd Dupont" <some...@somewhere.netwrote:

<snip>

惊喜,惊喜,它''正是他们在微软所做的,你会注意到,

例如,

IList< T:IList

IEnumerator< T :IEnumerator

ICollection< T:ICollection

等....
Surprise, surprise, it''s exactly what they did at Microsoft, you''ll notice,
for exemple, that
IList<T: IList
IEnumerator<T: IEnumerator
ICollection<T: ICollection
etc....



这是唯一的对于IEnumerator< T>。 IList< Tdoesn'n范围IList和

ICollection< Tdoesn'n扩展ICollection。


也就是说,使通用接口扩展为非泛型在许多情况下,一个是一个很好的解决方案。这就是我现在正在做的事情

协议缓冲区...


Jon

That''s only true for IEnumerator<T>. IList<Tdoesn''t extent IList and
ICollection<Tdoesn''t extend ICollection.

That said, making the generic interface extend a non-generic one is a
fine solution in many cases. It''s what I''m doing at the moment with
Protocol Buffers...

Jon


2008年7月23日星期三22:46:41 -0700,Jon Skeet [C#MVP]< sk *** @ pobox.com>

写道:
On Wed, 23 Jul 2008 22:46:41 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:

[...]

也就是说,使通用接口扩展为非通用接口是一个很好的解决方案在很多情况下。
[...]
That said, making the generic interface extend a non-generic one is a
fine solution in many cases.



注意,恕我直言,这样做假定非通用接口已经存在

。我不相信我会为

目的创建一个非通用的界面(据我所知,微软也没有),因为这样做

几乎最终会故意破坏使用

泛型的地步。


无论代码已经使用了什么具体形式的通用接口

必须已经知道类型参数,所以制作一个通用的方法来支持通用接口根本不应该是一个问题(假设这是'b $ b不适合使用具体形式声明的方法本身

通用接口,我认为通常就是这种情况。) />

Pete

Noting, IMHO, that doing so assumes the non-generic interface already
exists. I''m not convinced that I''d create a non-generic interface for the
purpose (and as far as I know, Microsoft didn''t either), since doing so
pretty much winds up intentionally defeating the point of using the
generic in the first place.

Whatever code''s already using the generic interface in its concrete form
must already know the type parameter, so making a generic method to
support the generic interface shouldn''t be a problem at all (assuming it''s
not suitable for the method itself to be declared using a concrete form of
the generic interface, which I think usually would be the case).

Pete


7月24日上午6:57,Peter Duniho < NpOeStPe ... @nnowslpianmk.com>

写道:
On Jul 24, 6:57 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:

那说,制作在很多情况下,泛型接口扩展非泛型接口是一个很好的解决方案。
That said, making the generic interface extend a non-generic one is a
fine solution in many cases.



注意,恕我直言,这样做假定非通用接口已经存在



Noting, IMHO, that doing so assumes the non-generic interface already
exists.



不一定。

Not necessarily.


我不相信我会创建一个非

目的的通用界面(据我所知,微软也没有),因为这样做

几乎结束故意打败使用点

首先是通用的。
I''m not convinced that I''d create a non-generic interface for the
purpose (and as far as I know, Microsoft didn''t either), since doing so
pretty much winds up intentionally defeating the point of using the
generic in the first place.



仅限某些情况。这取决于你感兴趣的API部分是否依赖于类型参数。例如,如果IList< Timplemented a(神秘的)

接口名为ICountable且具有Count属性,那么我的当前工作将会很方便。在我目前的代码中

我有时需要得到一个我知道的对象的数量

IList< Something但我不知道什么东西在编译 - 时间。

Only for some cases. It depends whether the parts of the API you''re
interested in depend on the type parameter. For instance, it would be
handy for my current work if IList<Timplemented a (mythical)
interface called ICountable with a Count property. In my current code
I sometimes need to get the count of an object which I know is an
IList<Somethingbut I don''t know what Something is at compile-time.


无论什么代码已经使用了具体形式的通用接口

必须已经知道类型参数,所以制作一个支持通用接口的泛型接口通用方法根本不应该是一个问题(假设它不适合使用混凝土声明方法本身的


形式的通用接口,我认为通常就是这种情况)。
Whatever code''s already using the generic interface in its concrete form
must already know the type parameter, so making a generic method to
support the generic interface shouldn''t be a problem at all (assuming it''s
not suitable for the method itself to be declared using a concrete form of
the generic interface, which I think usually would be the case).



不一定 - 参见上面的例子。在我的情况下,我收到

对象只是对象并且必须转换为*某些东西* - 但是没有类型信息我没有合适的类型。


另外,我需要假设一切都是动态的情况 -

这意味着我的非通用API实际上就像我的通用API一样富有,它只是缺乏精确的类型安全。

这意味着很多显式的接口实现等,这不是很好的b $ b非常好,但它是这种情况下最好的方式。我承认这不是一个非常常见的情况......


(该代码可在github上找到 -
http://github.com/jskeet/dotnet-prot。 ..rotocolBuffers

并查看IMessage.cs和IBuilder.cs。)


Jon

Not necessarily - see my example above. In my case I receive the
object as just "object" and have to cast to *something* - but there''s
no appropriate type I can cast to without the type information.

In addition, I need to suppose situations where everything''s dynamic -
which means my nongeneric API is actually just as rich in terms of
abilities as my generic API, it just lacks the precise type safety.
That means a lot of explicit interface implementation etc, which isn''t
terribly nice but it''s the best way available in the situation. I
acknowledge that it''s not a terribly common situation though...

(The code is available on github -
http://github.com/jskeet/dotnet-prot...rotocolBuffers
and look at IMessage.cs and IBuilder.cs.)

Jon


这篇关于Re:投射通用IFoo&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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