Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 214
aaa Avatar asked Oct 24 '25 03:10

aaa


1 Answers

Edited to use generics:

Try to use InjectionParameter instead:

container.RegisterType<Test>(new InjectionConstructor(new InjectionParameter<int?>(null)));
like image 197
jlvaquero Avatar answered Oct 27 '25 01:10

jlvaquero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!