【问题标题】:how to have multiple type for generics in child class如何在子类中为泛型设置多种类型
【发布时间】:2018-06-18 12:36:11
【问题描述】:

我有一个 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);
    }
}

现在我希望任何继承这个类的类都可以为 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)
    }

谢谢。

【问题讨论】:

  • 我不明白你的要求。您将 T 限制为 3 种互斥类型。
  • 我也不懂……你需要什么?
  • @DavidG,在子类中定义通用约束
  • @Haytam,我想在子类中定义泛型约束让子类控制泛型定义
  • 顺便说一句,这里的部分问题是您正试图将 WebAPI 控制器塑造成其他东西。保持您的业务逻辑完全不同,最好完全在不同的项目中。

标签: c# asp.net generics inheritance generic-programming


【解决方案1】:

我不完全确定我完全理解您的需求,但我想我有一个想法。
我认为你不应该使用泛型类,而是使用泛型方法:

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);
    }
}

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    这样写你的课(在课后添加&lt;T&gt;

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

    将泛型置于更高级别

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      • 2010-10-26
      • 2020-08-06
      • 2020-08-21
      • 2011-03-21
      相关资源
      最近更新 更多