Unity - How to pass null in the constructor parameter for nullable int

北战南征 提交于 2020-01-24 10:03:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!