如何在子类中具有泛型的多种类型

知识网

我有一个BaseController

public abstract class BaseController<T> : ApiController 
{

    protected APIResponseTO<T> _reponse;

    protected IHttpActionResult CreateResponse(HttpStatusCode httpStatus, T data)
    {
        _reponse = new APIResponseTO<T>()
        {
            HttpStatus = httpStatus,
            Data = data
        };
        return Ok(_reponse);
    }
}

现在,我希望继承该类的任何Class都可以为T定义多种类型,例如

 public class CustomerController : BaseController<T>
 {

    public IHttpActionResult Get()
    {

        var customers = _customerService.GetCustomers();
        //call Parent Class CreateResponse() to create IHttpActionResult object
        //here customers is IEnumerable<Customer>
        return CreateResponse(HttpStatusCode.Created, customers)
    }

    public IHttpActionResult Post([FromBody]Customer customer)
    {
        var custId= _customerService.AddCustomers();
        //call Parent Class CreateResponse() to create IHttpActionResult object
        //here customer is integer(Single Object)
        return CreateResponse(HttpStatusCode.Created, custId)
    }
}

我的要求是我可以在课堂上定义某种方式

public class CustomerController : BaseController<T> where T : Customer, IEnumerable<Customer>, int
 {
 }

或方法级别

 public IHttpActionResult Post<T>([FromBody]Customer customer)
  where T : int
    {
        var custId= _customerService.AddCustomers();
        //call Parent Class CreateResponse() to create IHttpActionResult object
        //here customer is integer(Single Object)
        return CreateResponse(HttpStatusCode.Created, custId)
    }

谢谢。

海塔姆

我不确定我是否完全了解您的需求,但我想我有个主意。
我认为您不应使用通用类,而应使用通用方法:

public class CustomerController : BaseController
{

    public IHttpActionResult Get()
    {

        var customers = new List<object>();
        return CreateResponse<List<object>>(HttpStatusCode.Created, customers);
    }

    public IHttpActionResult Post([FromBody]Customer customer)
    {
        int custId = 17;
        return CreateResponse<int>(HttpStatusCode.Created, custId);
    }
}

public abstract class BaseController : ApiController
{

    protected IHttpActionResult CreateResponse<TData>(HttpStatusCode httpStatus, TData data)
    {
        // Problem here is that BaseController is not a generic class anymore, so you can't store your responses but then again, why would you want to store them in a variable?
        var reponse = new APIResponseTO<TData>()
        {
            HttpStatus = httpStatus,
            Data = data
        };

        return Ok(reponse);
    }
}

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当泛型参数具有多种数据类型时,如何在rust中实现泛型?

Java类的字段可以不使用泛型而具有多种类型吗?

具有多种类型的泛型类会产生错误

如何从 dart flutter 中的单个泛型类返回多种类型的类类型?

Typescript中具有泛型的子类型约束

Dart 中泛型类的一个参数有多种类型

如何在Java的子类中更改泛型类型

如何在PyQt子类中没有泛型冲突的情况下使用泛型类型?

使用具有多种类型的泛型注册存储库 <T, T> c# dotnet core

如何在流中定义具有指定类型的所有字段可选的泛型类型

Java泛型:要求泛型是某种类型的子类

** kwargs中具有多种类型的类型注释

如何在C#中收集具有泛型类型的静态类的所有“实例”?

我如何在C#中转换具有多种类型的字符串

访问具有多种泛型类型的类中的静态变量

如何在Scala中设置绑定的类型参数以使数字具有泛型功能?

如何在具有泛型类型的类中启用协方差?

如何在C#中对具有抽象内部类型的泛型进行模式匹配?

在Swift 4中更新具有多种类型的标签

在TypeScript中定义具有多种类型的数组

Java中具有多种类型的值的映射

PDDL中的变量可以具有多种类型吗?

当道具在React,Typescript中可以具有多种类型时,如何传递特定道具?

如何为通讯录中的联系人创建具有多种类型的NSMutableDictionary?

如何将具有多种类型的参数作为参数传递给 Typescript 中的函数?

具有子类泛型的子类

如何创建泛型类接受多种类型 C#

具有多种类型的Scala变量

如何在Golang中为多种类型编写函数?