Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在子类中具有泛型的多个类型_C#_Asp.net_Generics_Inheritance_Generic Programming - Fatal编程技术网

C# 如何在子类中具有泛型的多个类型

C# 如何在子类中具有泛型的多个类型,c#,asp.net,generics,inheritance,generic-programming,C#,Asp.net,Generics,Inheritance,Generic Programming,我有一个基站控制器 public abstract class BaseController<T> : ApiController { protected APIResponseTO<T> _reponse; protected IHttpActionResult CreateResponse(HttpStatusCode httpStatus, T data) { _reponse = new APIResponseTO&l

我有一个基站控制器

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);
    }
}
公共抽象类BaseController:ApiController
{
保护性API反应;
受保护的IHttpActionResult CreateResponse(HttpStatusCode httpStatus,T数据)
{
_响应=新的APIResponseTO()
{
HttpStatus=HttpStatus,
数据=数据
};
返回Ok(正常响应);
}
}
现在我希望任何继承这个类的类都可以为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)
    }
}
公共类CustomerController:BaseController
{
public IHttpActionResult Get()
{
var customers=_customerService.GetCustomers();
//调用父类CreateResponse()以创建IHttpActionResult对象
//这里的顾客数不胜数
返回CreateResponse(HttpStatusCode.Created,客户)
}
公共IHttpActionResult帖子([FromBody]客户)
{
var custId=_customerService.AddCustomers();
//调用父类CreateResponse()以创建IHttpActionResult对象
//这里客户是整数(单个对象)
返回CreateResponse(HttpStatusCode.Created,custId)
}
}
我的要求是,我可以在类级别进行定义

public class CustomerController : BaseController<T> where T : Customer, IEnumerable<Customer>, int
 {
 }
公共类CustomerController:BaseController,其中T:Customer,IEnumerable,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 IHttpActionResult Post([FromBody]客户)
其中T:int
{
var custId=_customerService.AddCustomers();
//调用父类CreateResponse()以创建IHttpActionResult对象
//这里客户是整数(单个对象)
返回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);
    }
}
公共类CustomerController:BaseController
{
public IHttpActionResult Get()
{
var customers=新列表();
返回CreateResponse(HttpStatusCode.Created,客户);
}
公共IHttpActionResult帖子([FromBody]客户)
{
int custId=17;
返回CreateResponse(HttpStatusCode.Created,custId);
}
}
公共抽象类BaseController:ApiController
{
受保护的IHttpActionResult CreateResponse(HttpStatusCode httpStatus,TData数据)
{
//这里的问题是BaseController不再是泛型类,所以您不能存储响应,但话说回来,为什么要将它们存储在变量中?
var Response=新的APIResponseTO()
{
HttpStatus=HttpStatus,
数据=数据
};
返回Ok(响应);
}
}

希望这能有所帮助。

像这样编写您的类(在类后添加

公共类CustomerController:BaseController,其中T:Customer,IEnumerable,int{
}

将把通用的放在更高的层次上

我不理解您的要求。你把
T
限制为3种相互排斥的类型。我也不明白。。。这里需要什么?@DavidG,在Child中定义通用约束class@Haytam,我想在子类中定义泛型约束让子类控制genric定义顺便说一句,这里的部分问题是您试图将WebAPI控制器塑造成其他东西。保持您的业务逻辑完全不同,最好是在完全不同的项目中。
public class CustomerController<T> : BaseController<T> where T : Customer, IEnumerable<Customer>, int {
}