我创建一个自定义泛型类:C#泛型类型引起歧义
class Widget< T1, T2>
{
...
public bool Bar(T1 type1)
{
...
}
public bool Bar(T2 type2)
{
...
}
...
}
以下行,当然,创造一个模糊的通话编译错误:
Widget<int, int> Foo = new Widget<int, int>();
...
Foo.Bar(5);
...
有没有解决这个任何方式?有没有一个条款,我可以把“where:TypeOf(T1)!= TypeOf(T2)”,或任何方式使这种消除歧义?最好是int,int可用,但它不是必须的。
更新:
其实我对我自己发现了一个可接受的解决方案(我)这个问题,对于那些有兴趣谁
class Widget< T1, T2>
{
...
public bool Bar(object o)
{
if(o.GetType() == typeof(T1))
{
...
}
if(o.GetType() == typeof(T2))
{
...
}
}
...
}
我想知道什么是真实世界的情况下... – user76035 2010-02-23 21:57:51
在现实中它变成和无效的方法超载 – Perpetualcoder 2010-02-23 22:24:13
@wwosik:带有2个键的字典。 – 2010-02-24 16:28:49