Using default parameter values with Ninject 3.0

前端 未结 2 1712
无人共我
无人共我 2021-01-04 10:02

I have a class with a constructor having a parameter with a default value. With Ninject 2.2, it would honor the [Optional] attribute and work fine with no b

2条回答
  •  -上瘾入骨i
    2021-01-04 10:43

    You can avoid setting AllowNullInjection globally while still injecting a parameter only when a binding exists by configuring your binding like this:

    Kernel.Bind()
          .To()
          .WithConstructorArgument(typeof(IValidator), c => c.Kernel.TryGet>());
    

    [Optional] is not needed for that.

提交回复
热议问题