问题
I have a class that takes nullable int as parameter.
public class Test
{
public Test(int? p)
{
// ......
}
// ......
}
How do I resolve it using unity (passing null as parameter)?
myContainer.RegisterType<Test>(new InjectionConstructor(10));
This works passing 10 as value, but if I pass null, it throws exception.
回答1:
Edited to use generics:
Try to use InjectionParameter instead:
container.RegisterType<Test>(new InjectionConstructor(new InjectionParameter<int?>(null)));
回答2:
Use an InjectionParameter<T>
of the correct type, i.e.
container.RegisterType<Test>(new InjectionConstructor(new InjectionParameter<int?>(null)));
This has been tested in visual studio.
来源:https://stackoverflow.com/questions/18570168/unity-how-to-pass-null-in-the-constructor-parameter-for-nullable-int