2011-09-22 44 views
1

我知道这不是好习惯。使用NInject查找班级,但使用自己的参数构建班级

下面是一些代码之类的演示问题(但不实际工作):

public interface IBar {} 

public interface Bar : IBar {} 

public interface IFoo {} 

public class Foo : IFoo 
{ 
    public Foo(IBar bar) 
    { 
    } 
} 

public class InjectionModule : NinjectModule 
{ 
    public override void Load() 
    { 
     Bind<IFoo>().To<Foo>(); 
    } 
} 

public class MyApp 
{ 
     public void DoSomething() 
     { 
      // Get a foo with a particular bar 
      var foo1 = Kernel.Get<IFoo>(new Bar()); 

      // Get another foo with a different bar 
      var foo2 = Kernel.Get<IFoo>(new Bar()); 
     } 
} 

所以我试图做的是使用NInject到IFoo的绑定到富,但有我应用程序在运行时向构造函数提供Bar参数,而不是NInject解决IBar依赖性的通常做法。

回答

2
var foo1 = Kernel.Get<IFoo>(new ConstructorArgument("bar", new Bar()));