我正在为一个示例项目开发Interface我希望它尽可能通用,所以我创建了一个如下所示的界面
public interface IUserFactory
{
IEnumerable<Users> GetAll();
Users GetOne(int Id);
}
但后来发生了我必须复制接口以进行下面的操作
public interface IProjectFactory
{
IEnumerable<Projects> GetAll(User user);
Project GetOne(int Id);
}
如果你看上面差异只是他们返回的类型,所以我创建了类似下面的内容,但却发现我收到错误Cannot Resolve Symbol T
我做错了什么
public interface IFactory
{
IEnumerable<T> GetAll();
T GetOne(int Id);
}
答案 0 :(得分:11)
您需要使用通用interface / class,而不仅仅是generic methods:
public interface IFactory<T>
{
IEnumerable<T> GetAll();
T GetOne(int Id);
}
在接口/类上定义泛型类型可确保类在整个类中都是已知的(无论使用何种类型说明符)。
答案 1 :(得分:10)
在界面上声明类型:
public interface IFactory<T>
答案 2 :(得分:2)
编译器无法推断出T的用途。您还需要在类级别声明它。
尝试:
public interface IFactory<T>
{
IEnumerable<T> GetAll();
T GetOne(int Id);
}