传递类的接口会引发错误,但仅传递类可以正常工作

发布于 2024-10-31 22:26:23 字数 1293 浏览 2 评论 0原文

我有两个类:

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) 可以是用于代替 IEntityServiceEntityService 没有问题。

任何人都知道问题可能是什么?

谢谢,
哈罗德

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时模样 2024-11-07 22:26:23

您的构造函数参数和第二个类参数必须是相同的类型。即 TDefectService 。

如果你想使用接口,那么你的具体类应该定义如下...

public class JobDefectsController : DefectsController<JobDefect, IEntityService<JobDefect>>
{
    public JobDefectsController(IEntityService<JobDefect> service)
        : base(service)
    {
    }
}

要使用具体类,那么你需要这样做...

public class JobDefectsController : DefectsController<JobDefect, EntityService<JobDefect>>
{
    public JobDefectsController(EntityService<JobDefect> service)
        : base(service)
    {
    }
}

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...

public class JobDefectsController : DefectsController<JobDefect, IEntityService<JobDefect>>
{
    public JobDefectsController(IEntityService<JobDefect> service)
        : base(service)
    {
    }
}

To use the concrete class then you need to do this...

public class JobDefectsController : DefectsController<JobDefect, EntityService<JobDefect>>
{
    public JobDefectsController(EntityService<JobDefect> service)
        : base(service)
    {
    }
}
淡紫姑娘! 2024-11-07 22:26:23

修复了问题,这是 IEntityService 中没有函数 QueryEntityService 有的问题。我没有意识到这些类是在项目中构建的,现在感觉有点傻。

哦,好吧,无论如何,谢谢!

Fixed the problem, it was an issue with IEntityService not having the function Query in it whereas EntityService 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文