[英]Interface with the method of generic type
我有一個interface
interface Repository {
...
List<T> GetAll<T>();
...
}
我應該如何實現該方法? 編譯器說我不能做到如下所示,因為我返回的不是通用列表
public class EmployeeRepository : Repository {
...
public List<T> GetAll<T>() {
List<Employee> employees = new List<Employee>();
...
return employees;
}
使Repository
成為通用接口:
interface Repository<T> {
List<T> GetAll();
}
public class EmployeeRepository : Repository<Employee> {
public List<Employee> GetAll() {
List<Employee> employees = new List<Employee>();
return employees;
}
}
您還應該將接口存儲庫定義為通用類,例如
public interface IRepository<T> {
List<T> GetAll();
}
並且您的EmployeeRepository應該如下實現
public class EmployeeRepository : IRepository<Employee> {
public List<Employee> GetAll() {
List<Employee> employees = new List<Employee>();
return employees;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.