简体   繁体   中英

Ninject does not propagate exception

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
       }
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM