传递类的接口会引发错误,但仅传递类可以正常工作
我有两个类:
DefectsController
public abstract class DefectsController<TDefect, TDefectService> :
ApplicationController<DefectsController<TDefect, TDefectService>, TDefect>
where TDefectService : IEntityService<TDefect>
where TDefect : Defect
{
private readonly TDefectService defectService;
public DefectsController(TDefectService defectService)
: base(defectService)
{
this.defectService = defectService;
}
}
和 JobDefectsController (还有其他部分,这些只是示例)
public class JobDefectsController : DefectsController<JobDefect, IEntityService<JobDefect>>
{
public JobDefectsController(EntityService<JobDefect> service)
: base(service)
{
}
}
出于某种原因,如果我将 JobDefectsController 的构造函数更改为使用 IEntityService,那么如果我尝试访问 DefectsController 中的 defectService
它会抛出未知错误。
编辑:此错误:提供的泛型参数数量不等于泛型类型定义的数量。
参数名称:实例化
曾经有一个名为 JobDefectsService
的类,它继承自 IEntityService
,其接口 (IJobDefectService
) 可以是用于代替 IEntityService
和 EntityService
没有问题。
任何人都知道问题可能是什么?
谢谢,
哈罗德
I have two classes:
DefectsController
public abstract class DefectsController<TDefect, TDefectService> :
ApplicationController<DefectsController<TDefect, TDefectService>, TDefect>
where TDefectService : IEntityService<TDefect>
where TDefect : Defect
{
private readonly TDefectService defectService;
public DefectsController(TDefectService defectService)
: base(defectService)
{
this.defectService = defectService;
}
}
and JobDefectsController (there are other parts as well, these are just examples)
public class JobDefectsController : DefectsController<JobDefect, IEntityService<JobDefect>>
{
public JobDefectsController(EntityService<JobDefect> service)
: base(service)
{
}
}
For some reason if I change JobDefectsController's constructor to using IEntityService instead then if I try to access defectService
in the DefectsController it throws unknown errors.
Edit: This error: The number of generic arguments provided doesn't equal the arity of the generic type definition.
Parameter name: instantiation
There used to be a class called JobDefectsService
which inherited from IEntityService<JobDefect>
and the interface of that (IJobDefectService
) could be used in place of both IEntityService<JobDefect>
and EntityService<JobDefect>
without problems.
Anyone have any clue to what the problem could be?
Thanks,
Harold
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的构造函数参数和第二个类参数必须是相同的类型。即 TDefectService 。
如果你想使用接口,那么你的具体类应该定义如下...
要使用具体类,那么你需要这样做...
Your constructor parameter and your second class parameter need to be the same type. I.e.
TDefectService
.If you want to use the interface then your concrete class should be defined as follows...
To use the concrete class then you need to do this...
修复了问题,这是
IEntityService
中没有函数Query
而EntityService
有的问题。我没有意识到这些类是在项目中构建的,现在感觉有点傻。哦,好吧,无论如何,谢谢!
Fixed the problem, it was an issue with
IEntityService
not having the functionQuery
in it whereasEntityService
did. I did not realise that these classes were built within the project and now feel like a bit of a fool.Oh well, thanks anyway!