Iron Ninja
Iron Ninja

Reputation: 434

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?

Upvotes: 5

Views: 2083

Answers (1)

Remo Gloor
Remo Gloor

Reputation: 32725

You can define a binding like this:

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

Upvotes: 3

Related Questions