C#:方法的通用实现不满足接口
在这个 帖子我谈到了使用通用基类使我能够创建存储库类,而无需重复加载基本的管道代码。
每个存储库都可以通过接口访问。在下面的代码中,为了简洁起见,我将仅显示其中一种方法:
接口:
IQueryable<Suggestion> All { get; }
通用基类
public IQueryable<T> All
{
get { return _unitOfWork.GetList<T>(); }
}
具体类(实现接口并扩展通用基类)
public IQueryable<Suggestion> All
{
get { return _unitOfWork.GetList<Suggestion>(); }
}
我预计我能够简单地从具体类中剥离该方法,并且编译器将使用通用基类实现来代替并计算出旨在满足接口的方法。但不!
当我删除该方法时,我收到旧的“未实现接口成员”错误。
如果我做不到这一点,我使用通用基类的努力不是毫无意义吗?或者有办法解决这个问题吗?
In this post I talked about using a generic base class to enable me to create repository classes without duplicating loads of basic plumbing code.
Each Repository is accessed through an interface. In the code below, I will only show one of the methods for the sake of brevity:
Interface:
IQueryable<Suggestion> All { get; }
Generic base class
public IQueryable<T> All
{
get { return _unitOfWork.GetList<T>(); }
}
Concrete class (implements the interface and extends the generic base class)
public IQueryable<Suggestion> All
{
get { return _unitOfWork.GetList<Suggestion>(); }
}
I anticipated that I would be able to simply strip the method out of the concrete class, and the compiler would use the generic base class implementation instead and work out that was intended to satisfy the interface. But no!
When I strip the method out I get the old 'does not implement interface member' error.
If I can't do this, have my efforts to use a generic base class not been pointless? Or is there a way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使接口本身通用,然后在具体类中实现类型化版本吗?
我仍然建议使用通用接口,因为它可以让您免于重复,但这也有效。
Can you make the interface itself generic then implement a typed version in your concrete class?
I'd still suggest using the generic interface since it will save you from repeating yourself, but this works too.
使用 virtual 关键字并将您的接口放在具体的实现上。
Use the virtual keyword and put your interface on your concrete implementation..