Unity - 如何在可空 int 的构造函数参数中传递 null [英] Unity - How to pass null in the constructor parameter for nullable int

查看:76
本文介绍了Unity - 如何在可空 int 的构造函数参数中传递 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a class that takes nullable int as parameter.

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

    // ......
}

如何使用 unity(传递 null 作为参数)来解决它?

How do I resolve it using unity (passing null as parameter)?

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

这可以传递 10 作为值,但如果我传递 null,它会抛出异常.

This works passing 10 as value, but if I pass null, it throws exception.

推荐答案

编辑为使用泛型:

尝试使用 InjectionParameter 代替:

Try to use InjectionParameter instead:

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

这篇关于Unity - 如何在可空 int 的构造函数参数中传递 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆