3

我有一个将可为空的 int 作为参数的类。

public class Test
{
    public Test(int? p)
    {
        // ......
    }

    // ......
}

如何使用统一解决它(将 null 作为参数传递)?

myContainer.RegisterType<Test>(new InjectionConstructor(10));

这可以将 10 作为值传递,但如果我传递 null,则会引发异常。

4

2 回答 2

7

编辑使用泛型:

尝试使用 InjectionParameter 代替:

container.RegisterType<Test>(new InjectionConstructor(new InjectionParameter<int?>(null)));
于 2013-09-02T10:04:33.323 回答
5

使用InjectionParameter<T>正确的类型,即

container.RegisterType<Test>(new InjectionConstructor(new InjectionParameter<int?>(null)));

这已在视觉工作室中进行了测试。

于 2013-09-02T10:07:19.867 回答