我有一个以nullable int为参数的类.
public class Test
{
public Test(int? p)
{
// ......
}
// ......
}
Run Code Online (Sandbox Code Playgroud)
如何使用unity解析它(将null作为参数传递)?
myContainer.RegisterType<Test>(new InjectionConstructor(10));
Run Code Online (Sandbox Code Playgroud)
这可以传递10作为值,但如果我传递null,它会抛出异常.
编辑使用泛型:
尝试使用InjectionParameter:
container.RegisterType<Test>(new InjectionConstructor(new InjectionParameter<int?>(null)));
Run Code Online (Sandbox Code Playgroud)
使用InjectionParameter<T>正确的类型,即
container.RegisterType<Test>(new InjectionConstructor(new InjectionParameter<int?>(null)));
Run Code Online (Sandbox Code Playgroud)
这已经在visual studio中进行了测试.