简体   繁体   中英

Ninject Pass Delegate into WithConstructorArgument

I have a need to point to a method in Ninject bindings as part of a constructor argument. The constructor for the class looks like this:

MyObject(Func<Populator> param1, TimeSpan time)

I have been looking and haven't been able to find a way to bind the delegate for the Func. Is this even possible? Ninject wouldn't let me do this because its expecting an object as an argument, and won't take a delegate.

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

Is there a way to get this behavior to work in Ninject?

您可以像这样定义绑定:

Bind<Func<Populator>>().ToMethod(ctx => ctx.Kernel.Get<OtherWiredObject>().PopMethod);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM