rud*_*ter 1 c# generics types compiler-errors
我无法理解为什么以下代码无法编译:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
public interface ITestCondition<T> {
}
public class TestConditionBase<T>: ITestCondition<T> {
}
public class TestCondition<T>: TestConditionBase<T> {
}
public static class TestConditionExtension {
public static V Foo<V, T>(this V condition) where V: ITestCondition<T> {
return condition;
}
}
class Program {
static void Main(string[] args) {
new TestCondition<int>().Foo();
}
}
}
Run Code Online (Sandbox Code Playgroud)
它说它找不到"foo".但它没有通用类型就能完美运行.
编译器无法推断类型.明确指定它们:
new TestCondition<int>().Foo<TestCondition<int>, int>();
Run Code Online (Sandbox Code Playgroud)