What causes the Ninject.Activation.IRequest Target property to be null

853 Views Asked by At

Probably a simple question, but I did not find anything in the documentation or on SO that directly answers it.

I had to work with the Ninject When(Func<IRequest, bool> condition) extension method for the first time, and got hung up for a bit with null object exceptions. I figured out that the IRequest.Target property is sometimes null and I've seen some other examples using When that check for a null Target as well.

I'm curious about the conditions under which Ninject executes the callback without having a value for Target. When creating a binding of the form

Bind<T1>()
   .To<T2>
   .When(r => SomeTest(r.Target));

I initially and erroneously assumed that there would always be a valid Target of type T1 when the binding was being executed.

1

There are 1 best solutions below

1
On BEST ANSWER

It is null for the root object because there is no target in this case. (kernel.Get<MyCompositionRoot>())