简体   繁体   English

通用 controller class 与 inheritance .NET

[英]Generic controller class with inheritance .NET

I am making a custom controller class, from which all my other controller classes will inherit in.Net Framework 4.8, Web API I am making a custom controller class, from which all my other controller classes will inherit in.Net Framework 4.8, Web API

public class BaseApiController<T> where T: class, ApiController
{
    protected T service;

    public BaseApiController(T service)
    {
        this.service = service;
    }
}

This gives a compile-time error这给出了编译时错误

Error CS0450 'ApiController': cannot specify both a constraint class and the 'class' or 'struct' constraint错误 CS0450“ApiController”:不能同时指定约束 class 和“类”或“结构”约束

I know I am missing small detail with the right syntax.我知道我缺少正确语法的小细节。
T is a service, which is individual for every controller and I want to make sure it is always a reference type. T是一项服务,对于每个 controller 都是单独的,我想确保它始终是一个引用类型。 I also want to inherit from the main ApiController class.我还想从主ApiController class 继承。

If I do it like this, there are no errors (This works for me):如果我这样做,就没有错误(这对我有用):

public class BaseApiController<T> : ApiController
{
    protected T service;

    public BaseApiController(T service)
    {
        this.service = service;
    }
}

But I want the additional T logic for reference type.但我想要引用类型的附加T逻辑。

I want to use it like this, eg:我想这样使用它,例如:
Note: This how I use it and it works.注意:这是我使用它的方式并且它有效。

[Authorize]
[RoutePrefix("api/[controller]")]
public class MyOtherController : BaseApiController<IMyOtherService>
{
    public MyOtherController(IMyOtherService myOtherService) : base(myOtherService)
    {

    }

    [HttpPost]
    public IHttpActionResult DoSomething(Model requestModel)
    {
        var result = this.service.SetPaymentStatusUK(model: requestModel);
        return Ok();
    }
}

I just want to know how to add the T check for reference type in the BaseApiController class.我只想知道如何在BaseApiController class 中添加T检查引用类型。

If I understand your question right, the issue is that you applied ApiControler as a Constraint to T. When you wanted to apply it as a Ancestor of BaseApiController<T> .如果我正确理解您的问题,那么问题是您将ApiControler作为约束应用到 T。当您想将其应用为BaseApiController<T>的祖先时。

As a constraint ApiControler it is not valid, because you already applied the class constraint.作为约束ApiControler它无效,因为您已经应用了class约束。 class include all classes, including ApiControler and every other class that has yet to be written. class包括所有类,包括 ApiControler 和所有其他尚未编写的 class。 It is a less restrictive constraint the ApiControler.这是 ApiControler 限制较少的约束。 You only use a specific type or inferface as constraint, if you want all possible T's the absolutely positively have that inheritance/Interface Implementation.您只使用特定类型或接口作为约束,如果您希望所有可能的 T 绝对肯定具有该继承/接口实现。

According to the Syntax examples , you first inherit, then you constrain.根据语法示例,您首先继承,然后是约束。 So:所以:

public class BaseApiController<T> : ApiController where T: class
{
  //all the code
}

Might be the droid you are looking for.可能是您正在寻找的机器人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM