根据这个代码:
class C
{
C()
{
Test<string>(A); // fine
Test((string a) => {}); // fine
Test((Action<string>)A); // fine
Test(A); // type arguments cannot be inferred from usage!
}
static void Test<T>(Action<T> a) { }
void A(string _) { }
}
编译器抱怨 Test(A)
无法将 T
算成 string
。
对我来说,这似乎是一个非常简单的情况,我发誓我已经在其他通用实用程序和扩展函数中使用了更复杂的推理。我错过了什么?
更新1: 这在 C # 4.0编译器中。我在 VS2010中发现了这个问题,上面的示例来自我在 LINQPad 4中制作的一个最简单的复制品。
更新2: 添加了一些更多的示例到工作列表中。