【发布时间】:2013-04-14 15:46:17
【问题描述】:
假设我有一个基类
class Document
{
//some properties
}
和 两个派生类:
public class Order:Document
{
//some properties
}
public class Request:Document
{
//some properties
}
现在我想制作一个具有服务层的分层应用程序。我也想要一个基础服务层接口,例如:
public interface IBaseService<T> where T:Document
{
IList<T> GetAll();
}
我的层服务类实现它:
public SaleService:IBaseService<Document>;
所以我必须定义IList<Document> GetAll() 来实现基本服务,但我希望我的SaleService 有IList<Order> GetAll() 和IList<Request> GetAll() 方法,而不是IList<Document> GetAll()。
我该怎么做?
【问题讨论】:
-
你不能,因为你在
IBaseService<T>接口中设置了一个IList<T>方法。 -
听起来你必须在同一个类上有两个不同的
GetAll()方法,具有不同的返回类型。你不能在 C# 中做到这一点。请尝试澄清您的问题。 -
是的,我在同一个类上有两个不同的 GetAll() 方法,具有不同的返回类型。
-
你应该简单地使用
public SaleService:IBaseService<Order> ...;,否则<T>参数是多余的。
标签: c# generics inheritance interface