使用StandardKernel.Get()时Ninject获取Null

时间:2013-08-29 20:12:19

标签: dependency-injection ninject ioc-container

我的主界面下有两个分支, 并尝试在这里使用Ninject IOC根据一些condiction动态加载适当的类。这是我对班级结构和问题的描述

  Interface I 
  {
      Void method1 ();
      Void method2 ();
      Void method3 ();
  }

Abstract class A : I
{
      Abstract void method1 ();            
      Abstract void method2 ();            
      Void Method3() {  //Some implementation can be shared by all classes    }
}

分支1

Abstract Class B : A
{
      void method1 () {   //Some implementation can be shared by B1, B2   }
}

B1和B2是branch1中的最低级别类,将在代码中使用

Class B1 : B
{
      Void Method2() {  //Some implementation    }
}

Class B2 : B
{
      Void Method2() {  //Some implementation    }
}

但是还有另一个分支从抽象类A的居民开始,如下所示

分支2

Abstract Class C : A
{
      void method1 () 
      {   
         //Some implementation which is different from in B.Method1 
         //and can be shared by C1                 
      }
}

C1是branch2中的最低级别类,将在代码中使用

Class C1 : C
{
       Void Method2() {  //Some implementation    }
}

我使用Ninject绑定并动态加载类,如下所示:

Bind<I>().To<B1>();
Bind<I>().To<B2>();
Bind<I>().To<C1>();

并使用kernel.Get<I>();

检索它们

现在问题是B1和B2被重新调整好了,但是当我检索C1时,它总是抛出一个空错误。我在这里错过了什么吗?任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

评论的详细信息太长,所以我不得不把它放在另一个部分

当我使用手表时,我看到:值不能为null。参数名称:source

stackTrace是:

 at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
   at ThePoint.Ingestion.Abstract.FeedsProcessor..ctor() in c:\Users\houyang\Documents\GitHub\The-Point-Redesign\ThePoint.Ingestion\Abstract\FeedsProcessor.cs:line 34
   at ThePoint.Ingestion.Abstract.WebServiceFeedsProcessor..ctor()
   at ThePoint.Ingestion.Concrete.GannettLocalProcessorTest..ctor()
   at DynamicInjector08125ba6b65f454d9309fa177f39cd2d(Object[] )
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:\Projects\Ninject\ninject\src\Ninject\Activation\Providers\StandardProvider.cs:line 97
   at Ninject.Activation.Context.Resolve() in c:\Projects\Ninject\ninject\src\Ninject\Activation\Context.cs:line 157
   at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:line 386
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 37