Ninject 不传播异常
我正在使用 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
}
}
I'm using Ninject to create an instance of class which has constructor parameters. This class checks it's parameters value and if it's not valid it throws an exception. The only problem I'm facing is that Ninject swallows the exception and returns null for my object. I would like to have my excpetion propagated like other exceptions in c#. Is there a way to do this?
The code looks like this (just an example, this is not the actual code)
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法重现这种行为。至少当前发布的版本向调用者抛出异常。此外,您的代码甚至无法编译,因为 null 作为 ConstructorArgument 的参数不明确
I cannot reproduce this behavior. At least the current released version throws the exception to the caller. Additionally, your code won't even compile as null is ambiguous as argument for ConstructorArgument