微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用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来覆盖此行为.

原文地址:https://www.jb51.cc/csharp/93443.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐