在IProvider中引用ParentRequests构造函数参数

时间:2016-07-31 11:17:40

标签: c# dependency-injection ninject

有没有办法在kernel.Bind<>().ToMethod()内引用父的构造函数参数(实际上是IProvider)?

我的情况如下:

public class Bar{
   public Bar(ILogger logger) {}
}

public class Foo{
   public Foo(ILogger logger, Bar bar){}
}

让我说我有一些记录器:

public class TopLevelLogger{
}

public class ChildLogger{
   public ChildLogger(ILogger parent){}
}

当我设置我的绑定时,我需要获得对父类的记录器的引用:

kernel = new StandardKernel();
kernel.Bind<ILogger>.ToMethod(ctx => 
{
   if (null == ctx.Request.ParentRequest.ParentRequest) 
      // at top level (ie Foo)
      return new TopLevelLogger();

   // I know I've already created a TopLevelLogger, how do I get access to it?
  var parentLogger = ???????
  return new ChildLogger(parentLogger);
});  

0 个答案:

没有答案