Ninject Pass委托进入WithConstructorArgument

Iro*_*nja 5 c# ninject ioc-container ninject-2

我需要指向Ninject绑定中的方法作为构造函数参数的一部分.该类的构造函数如下所示:

MyObject(Func<Populator> param1, TimeSpan time)
Run Code Online (Sandbox Code Playgroud)

我一直在寻找并且无法找到一种方法来绑定Func的委托.这甚至可能吗?Ninject不会让我这样做,因为它期望一个对象作为一个参数,并且不会接受代表.

Bind<IInterface>()
      .To<MyObject>()
      .InSingletonScope()
      .WithConstructorArgument
            ("param1", ctx => ctx.Kernel.Get<OtherWiredObject>().PopMethod)
      .WithConstructorArgument
            ("time", new TimeSpan(0,30,0));
Run Code Online (Sandbox Code Playgroud)

有没有办法让这种行为在Ninject中起作用?

Rem*_*oor 3

您可以像这样定义绑定:

Bind<Func<Populator>>().ToMethod(ctx => ctx.Kernel.Get<OtherWiredObject>().PopMethod);
Run Code Online (Sandbox Code Playgroud)