【发布时间】:2016-08-04 20:38:04
【问题描述】:
到目前为止,C# 推理对我来说一直很有效。我创建了一个测试示例来简化案例。
class Parent
{
public void InferrenceTesting<T>() where T : Parent
{
}
}
class Child : Parent
{
public void Test()
{
//this line gives me a compiler error : The type arguments for method 'Parent.InferrenceTesting<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly.
this.InferrenceTesting();
}
}
我已经阅读了很多关于推理的文章,但我不知道为什么这不起作用。
【问题讨论】:
-
编译器没有什么可推断的。 方法是通用的,而不是类,所以没有信息告诉编译器
T应该是什么类型。 -
尝试使用参数,因为@DStanley 声明编译器没有什么可推断的。
标签: c# generics inferred-type