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) )
{
...
}
}
...
}