简单(?)泛型问题 [英] Simple (?) generics question

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

问题描述




我有一个要求,我认为,非常简单,但实际上

对我来说是不可能的。


我在这里阅读了一些帖子和MS的语言规范,但是没有

得到解决方案。


我在java中编写了部分代码,因为它在那里非常简单:


公共类测试{

void test(){

IOption< ColoriOpt = new Option< Color>(Color.BLACK);

IOption< FontfOpt = new Option< Font>(Font.getFont(" xyz"));

列表< IOption<?> list = new ArrayList< IOption<?>>();

list.add(iOpt);

list.add(fOpt);

for(IOption<?option:list){

System.out.println(option.result()。toString()) ;

}

}

}

它会创建一个选项列表并添加一些不同的选项。最后

它迭代列表并打印所有

结果的字符串表示。接口IOption和类选项非常简单

用于演示目的:


公共接口IOption< T {

T结果();

}


公共类选项< Timplements IOption< T {

private T _val;


公共选项(T val){

_val = val;

}


public T result(){

return _val;

}

}

我无法得到这在C#中工作,因为我没有找到那个通配符 -

java的东西。我错过了什么?或者使用

泛型是不是可能?任何想法?


提前致谢,


奥拉夫

Hi,

I have a requirement that, I thought, was very simple, but in fact
turned out as impossible for me.

I read some posts here and the language specs from MS, but couldn''t
get a solution.

I coded the part in java, because it''s very straightforward there:

public class Test {
void test(){
IOption<ColoriOpt = new Option<Color>(Color.BLACK);
IOption<FontfOpt = new Option<Font>(Font.getFont("xyz"));
List<IOption<?>list = new ArrayList<IOption<?>>();
list.add(iOpt);
list.add(fOpt);
for (IOption<?option : list) {
System.out.println(option.result().toString());
}
}
}
It creates a list of options and adds some of different kind. Finally
it iterates the list and prints the string representation of all
results. The interface IOption and the class Option are very simple
for demonstration purposes:

public interface IOption<T{
T result();
}

public class Option<Timplements IOption<T{

private T _val;

public Option(T val) {
_val = val;
}

public T result() {
return _val;
}
}
I couldn''t get this to work in C# because I didn''t find that wildcard-
thingy of java. What am I missing? Or isn''t it possible using
generics? Any idea?

Thanks in advance,

Olaf

推荐答案

12月12日下午2:30,Olaf Krumnow< okrum ... @ gmx.dewrote:


< snip>
On Dec 12, 2:30 pm, Olaf Krumnow <okrum...@gmx.dewrote:

<snip>

我无法在C#中使用它,因为我没有找到那个通配符 -

java的东西。我错过了什么?或者使用

泛型是不是可能?任何的想法?
I couldn''t get this to work in C# because I didn''t find that wildcard-
thingy of java. What am I missing? Or isn''t it possible using
generics? Any idea?



这是不可能的 - IOption< Fooand IOption< Barare有效地

完全不同的类型。


你*可以*做的是使IOption< Talso实现一个非通用的
IOption接口,并创建一个List< IOption> ;.

Jon

It''s not possible - IOption<Fooand IOption<Barare effectively
completely different types.

What you *could* do is make IOption<Talso implement a non-generic
IOption interface, and create a List<IOption>.

Jon


除此之外,它不会很快被放入.NET

。我在MS的最后一次MVP峰会(以及那之前的一次)上与MS的一些人(我甚至问过Mads Torgersen

)进行了交谈,并且他们保持

tip toeing周围。当然,没有太多的喧嚣

(这就是为什么他们现在不想把它放进去),但它会b / b
有趣,至少可以说。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Jon Skeet [C#MVP]" < sk *** @ pobox.com写信息

新闻:82 *************************** ******* @ y5g2000h sf.googlegroups.com ...
To add to this, it''s not something that is likely to be put into .NET
anytime soon. I spoke with some people at MS (I even asked Mads Torgersen
about it) at the last MVP summit (and the one before that), and they keep
tip toeing around it. Granted, there isn''t a great deal of clamor for it
(which is why they aren''t interested in putting it in right now), but it
would be interesting, to say the least.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:82**********************************@y5g2000h sf.googlegroups.com...

12月12日下午2:30,Olaf Krumnow< okrum ... @ gmx.dewrote:


< snip>
On Dec 12, 2:30 pm, Olaf Krumnow <okrum...@gmx.dewrote:

<snip>

>我无法在C#中使用它因为我没有找到那个通配符的东西。我错过了什么?或者使用
泛型是不是可能?任何的想法?
>I couldn''t get this to work in C# because I didn''t find that wildcard-
thingy of java. What am I missing? Or isn''t it possible using
generics? Any idea?



这是不可能的 - IOption< Fooand IOption< Barare有效地

完全不同的类型。


你*可以*做的是使IOption< Talso实现一个非通用的
IOption接口,并创建一个List< IOption> ;.

Jon


It''s not possible - IOption<Fooand IOption<Barare effectively
completely different types.

What you *could* do is make IOption<Talso implement a non-generic
IOption interface, and create a List<IOption>.

Jon



12月12日下午4:08,Nicholas Paldino [.NET / C#MVP]"

< m ... @ spam.guard.caspershouse.comwrote:
On Dec 12, 4:08 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:

除此之外,它不是可能的很快就会进入.NET

。我在MS的最后一次MVP峰会(以及那之前的一次)上与MS的一些人(我甚至问过Mads Torgersen

)进行了交谈,并且他们保持

tip toeing周围。当然,没有太多的喧嚣

(这就是为什么他们现在不想把它放进去),但它会b / b
有趣,至少可以说。
To add to this, it''s not something that is likely to be put into .NET
anytime soon. I spoke with some people at MS (I even asked Mads Torgersen
about it) at the last MVP summit (and the one before that), and they keep
tip toeing around it. Granted, there isn''t a great deal of clamor for it
(which is why they aren''t interested in putting it in right now), but it
would be interesting, to say the least.



嗯,它在某种程度上已经在.NET中可用 - CLR

理解协变/逆变接口,但它们在C#中暴露出来的不是



Eric Lippert一直在为C#4写博客 - 他们是

当下非常仔细地考虑他们。个人

我不完全确定这是值得的额外复杂性,但我可以

说服:)


Jon

Well, it''s already available in .NET itself to some extent - the CLR
understands covariant/contravariant interfaces, but they''re not
exposed in C#.

Eric Lippert has been blogging about them for C# 4 though - they''re
certainly considering them very carefully at the moment. Personally
I''m not entirely sure it''s worth the extra complexity, but I could be
persuaded :)

Jon


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

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