假设有以下通用接口和实现类:
```java public interface MyInterface { public void doSomething(T t); } public class MyImplementation implements MyInterface { public void doSomething(String s) { System.out.println("Doing something with " + s); } } ```public interface IRepository<T> {
// U has to be of type T of a subtype of T
IQueryable<U> Find<U>() where U : T;
}
public class PersonRepository : IRepository<Employee> {
}
我该如何在不指定U的情况下调用Find方法?
var repository = new EmployeeRepository();
// Can't be done
IQueryable<Employee> people = repository.Find();
// Has to be, but isn't Employee a given in this context?
IQueryable<Employee> people = repository.Find<Employee>();
// Here I'm being specific
IQueryable<Manager> managers = repository.Find<Manager>();
换句话说,如何进行类型推断?谢谢!