加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用Ninject 3.0的默认参数值

发布时间:2020-12-15 03:54:11 所属栏目:百科 来源:网络整理
导读:我有一个类的构造函数具有一个默认值的参数.使用Ninject 2.2,它将遵守[可选]属性并且正常工作,没有对声明为如下的构造函数定义绑定: public EmployeeValidator([Optional] IValidatorPersonName personNameValidator = null) 由于升级到Ninject 3.0,此对象
我有一个类的构造函数具有一个默认值的参数.使用Ninject 2.2,它将遵守[可选]属性并且正常工作,没有对声明为如下的构造函数定义绑定:
public EmployeeValidator([Optional] IValidator<PersonName> personNameValidator = null)

由于升级到Ninject 3.0,此对象的构造失败,并显示一条消息,指出提供程序返回null:

Test method ValidatorIsolated.Tests.EmployeeValidatorTest.CreateEmployeeValidatorTest threw exception:

Ninject.ActivationException: Error activating IValidator{PersonName} using conditional implicit self-binding of IValidator{PersonName}

Provider returned null.

Activation path:

2) Injection of dependency IValidator{PersonName} into parameter personNameValidator of constructor of type EmployeeValidator

1) Request for IValidator{Employee}

Suggestions:

1) Ensure that the provider handles creation requests properly.

当参数的默认值存在时,[可选]属性是否仍然保持有效,并且使用可选参数(如此)处理注入的最佳方法是什么?

解决方法

在这种情况下,可选属性被忽略,因为始终有默认值可用 – 但提供的值为null.默认情况下,Null不是允许的值.

您可以通过将NinjectSettings.AllowNullInjection设置为true来覆盖此行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读