Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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# NInject:使用两个参数解析构造函数注入_C#_Ninject - Fatal编程技术网

C# NInject:使用两个参数解析构造函数注入

C# NInject:使用两个参数解析构造函数注入,c#,ninject,C#,Ninject,我创建了以下接口和类: interface IInterpreter. interface IViz. interface IVizDescriptor. 在哪里, class IntOne : IInterpreter. 以及 以及 我发现我能够在IInterpreter和IntOne之间创建绑定,以便在请求VizOne时,将IntOne注入第一个参数构造函数 问题是没有办法为VizDescOne创建合适的绑定 VizDescOne的构造函数参数太依赖于每种情况,我无法为其创建绑定 是否有

我创建了以下接口和类:

interface IInterpreter.
interface IViz.
interface IVizDescriptor.
在哪里,

class IntOne : IInterpreter.
以及

以及

我发现我能够在
IInterpreter
IntOne
之间创建绑定,以便在请求
VizOne
时,将
IntOne
注入第一个参数构造函数

问题是没有办法为
VizDescOne
创建合适的绑定<代码> VizDescOne的构造函数参数太依赖于每种情况,我无法为其创建绑定

是否有办法手动提供
并解析
VizOne(IntOne,IVizDescriptor)
构造函数


但是,
IVizDescriptor
太依赖于任何具体情况

您有很多选择:

  • 您可以使用构造函数将IVizDescriptor绑定到VizDescriptor 论据:

    kernel.Bind<IVizDescriptor>().To<VizDescOne>()
          .WithConstructorArgument("title", "someTitle").WithConstructorArgument("type", typeof(int))...
    
    kernel.Bind()到()
    .WithConstructorArgument(“title”,“someTitle”)。WithConstructorArgument(“type”,typeof(int))。。。
    
  • 您可以将IVizDescriptor绑定到常量:

    IVizDescriptor vizDescOne = new VizDescOne(...);
    kernel.Bind<IVizDescriptor>().ToConstant(vizDescOne);
    
    IVizDescriptor-vizDescOne=新的vizDescOne(…);
    kernel.Bind().ToConstant(vizDescOne);
    
  • 您可以将IVizDescriptor绑定到方法:

    kernel.Bind<IVizDescriptor>().ToMethod(o=> new VizDescOne(...));
    
    kernel.Bind().ToMethod(o=>newVizDescone(…);
    
  • 您可以阅读有关这些选项的更多信息,以及更多信息。


    作为旁注,我真的建议您阅读@Steven comment和他链接的文章,因为如果构造函数参数是do运行时值,您应该重新考虑您的设计

    我希望
    title
    type
    desc
    等参数都是运行时值;在应用程序的生命周期内,它们不是恒定的。内核是什么?ninject文档似乎相当难以捉摸。
    IVizDescriptor vizDescOne = new VizDescOne(...);
    kernel.Bind<IVizDescriptor>().ToConstant(vizDescOne);
    
    kernel.Bind<IVizDescriptor>().ToMethod(o=> new VizDescOne(...));