Ninject不传播异常
本文关键字:异常 传播 Ninject | 更新日期: 2023-09-27 18:08:37
我使用Ninject来创建一个具有构造函数参数的类实例。这个类检查它的参数值,如果无效则抛出异常。我面临的唯一问题是Ninject吞下异常并为我的对象返回null。我想让我的异常像c#中的其他异常一样传播。有办法做到这一点吗?
代码看起来像这样(只是一个例子,这不是实际的代码)
<>之前
public class Module : NinjectModule
{
public override void Load()
{
Bind<IMyClass>().To<MyClass>();
}
}
public class MyClass : IMyClass
{
public MyClass(object value)
{
if(value == null)
throw new ArgumentNullException("value");
}
}
public class Program
{
public void Method()
{
object myObject = null;
IMyClass instance = GetKernel().
Get<IMyClass>(new ConstructorArgument("value", myObject));
// here the instance variable is null and my
// exception has been swallowed
}
}
我无法复制此行为。至少当前发布的版本会向调用者抛出异常。此外,您的代码甚至不会编译,因为null作为ConstructorArgument