tags:

views:

73

answers:

1

Resolving a class that has multiple constructors with NInject doesn't seem to work.

public class Class1 : IClass
{
public Class1(int param) {...}
public Class1(int param2, string param3) { .. }
}

the following doesn’t seem to work:

IClass1 instance =
    IocContainer.Get<IClass>(With.Parameters.ConstructorArgument(“param”, 1));

The hook in the module is simple, and worked before I added the extra constructor: Bind().To();

Thanks in advance...

A: 

The reason that it doesn't work is that manually supplied .ctor arguments are not considered in the .ctor selection process. The .ctors are scored according to how many parameters they have of which there is a binding on the parameter type. During activation, the manually supplied .ctor arguments are applied. Since you don't have bindings on int or string, they are not scored. You can force a scoring by adding the [Inject] attribute to the .ctor you wish to use.

Ian Davis