Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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#_.net_Generics_Asp.net Web Api2 - Fatal编程技术网

C# 泛型类型约束构造函数限制,需要建议吗

C# 泛型类型约束构造函数限制,需要建议吗,c#,.net,generics,asp.net-web-api2,C#,.net,Generics,Asp.net Web Api2,我试图根据传递的类型参数在类的构造函数中实例化一个对象,以创建所述类的实例 public class DefaultController<TEntity, TStrategy> : ApiController, IDisposable where TEntity : class, IEntity where TStrategy: BaseStrategy<TEntity> { public IStrategy<TEntity> strategy {

我试图根据传递的类型参数在类的构造函数中实例化一个对象,以创建所述类的实例

public class DefaultController<TEntity, TStrategy> : ApiController, IDisposable where TEntity : class, IEntity where TStrategy: BaseStrategy<TEntity>
{

    public IStrategy<TEntity> strategy { get; set; }


    public DefaultController()
    {
        //strategy = ??
    }
}
如您所见,
BaseStategy
没有无参数构造函数,这使得在
DefaultController

我试图实现的是,从扩展
DefaultController
的类中,配置要使用的策略

public class UserController : DefaultController<User, GreatStrategy<User>>
{
    //   GreatStrategy extends BaseStrategy
}
public类UserController:DefaultController
{
//GreatStrategy扩展了基本策略
}
有什么办法可以让魔法发挥作用,或者其他方法


谢谢。

DefaultController需要无参数构造函数有什么原因吗?你可以试试:

public DefaultController(IStrategy<IEntity> strategy) : base(strategy)
{
    // Do stuff
}
public DefaultController(策略策略):基础(策略)
{
//做事
}
然后在UserController构造函数中:

public UserController() : base(new GreatStrategy<User>()) {
    // Do stuff
}
public UserController():base(新的GreatStrategy()){
//做事
}
您是否在应用程序中使用IoC?如果是这样,您可以注册您的策略并将它们注入控制器构造函数,这样您就不必在基本构造函数调用中启动新的策略


希望这能给您提供一些前进的指导。

我最终修改了默认控制器的构造函数以及扩展它的所有控制器的构造函数:

    public DefaultController(IStrategy<TEntity> _strategy)
    {
        strategy = _strategy; // 'strategy' is a local member implementing IStrategy...
    }
public DefaultController(IStrategy\u策略)
{
strategy=_strategy;//“strategy”是一个本地成员,负责实施IStrategy。。。
}
扩展默认控制器的控制器具有如下构造函数:

public class UserController : DefaultController<User>
{
    public UserController()
        : base (new GreatStrategy<User>(new Repository<User>()))
    {
        //GreatStrategy extends BaseStrategy...
    }

}
public类UserController:DefaultController
{
公共用户控制器()
:base(新的GreatStrategy(新存储库()))
{
//GreatStrategy扩展了基本策略。。。
}
}

我刚刚在
DefaultController
上添加了约束
,其中TStrategy:BaseStrategy,new()
,它可以很好地处理您当前的代码。然后我可以编写
strategy=newtstrategy()。我误解你的问题了吗?@Enigmativity如果你用
UserController
复制这个例子,你会得到错误:
必须是一个非抽象类型,带有公共无参数构造函数,才能将它用作参数“TStrategy”
。这就是我提到的问题。谢谢你的指导。我最终修改了DefaultController和扩展它的所有其他控制器的contsructor。
public class UserController : DefaultController<User>
{
    public UserController()
        : base (new GreatStrategy<User>(new Repository<User>()))
    {
        //GreatStrategy extends BaseStrategy...
    }

}