我創建一個自定義泛型類: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