如何在子 class 中为泛型设置多个类型
how to have multiple type for generics in child class
我有一个 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 的 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)
}
}
我的要求是我可以在 class 级别
以某种方式定义
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)
}
谢谢。
我不确定我是否完全理解您的需求,但我想我有一个想法。
我认为你不应该使用通用的 class 而是使用通用的方法:
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);
}
}
希望对您有所帮助。
像这样写你的 class(在 class 之后也添加 <T>
)
public class CustomerController<T> : BaseController<T> where T : Customer, IEnumerable<Customer>, int {
}
将泛型置于更高级别
我有一个 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 的 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)
}
}
我的要求是我可以在 class 级别
以某种方式定义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)
}
谢谢。
我不确定我是否完全理解您的需求,但我想我有一个想法。
我认为你不应该使用通用的 class 而是使用通用的方法:
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);
}
}
希望对您有所帮助。
像这样写你的 class(在 class 之后也添加 <T>
)
public class CustomerController<T> : BaseController<T> where T : Customer, IEnumerable<Customer>, int {
}
将泛型置于更高级别