When using Ninjects ConstructorArgument you can specify the exact value to inject to specific parameters. Why can’t this value be null, or how can I make it work? Maybe it’s not something you’d like to do, but I want to use it in my unit tests.. Example:
public class Ninja
{
private readonly IWeapon _weapon;
public Ninja(IWeapon weapon)
{
_weapon = weapon;
}
}
public void SomeFunction()
{
var kernel = new StandardKernel();
var ninja = kernel.Get<Ninja>(new ConstructorArgument("weapon", null));
}
Looking at the source (and the stack trace I got by reproing which you omitted :P)
This is because it’s binding to a different overload of the
ConstructorArgumentctor than the normal usage (i.e., where you’re passing a Value Type or a non-null Reference Type) does.The workaround is to cast the null to Object:-
Ninject 2 source:
Repro:
Lesson? You’d be crazy not to download the latest source and look at it. Great comments, nice clean codebase. Thanks again to @Ian Davis for that tip/prodding!