How to get all instances from the module loaded in a Ninject kernel?

Ninject provides convenient way for separating bindings into modules and also the GetAll<T>() method of the IResolutionRoot to get all instances for the given type.

I have multiple modules registering multiple classes implementing the same type, eg:

interface IFoo { }
class FooA : IFoo { } // in Module1
class FooB : IFoo { } // in Module2
class FooC : IFoo { } // in Module2

Is it possible to retrieve only the instance of the FooC by requesting IFoo after loading Module2 in the kernel with the Module1 already loaded? Something like:

StandardKernel kernel = new StandardKernel(new Module1()); // Module1 registered

Module2 module2 = new Module2();
kernel.Load(module2);
// kernel.GetAll<IFoo>().Where( ... from Module2 ... ); 

The only thing I thought of is to get all the registered bindings by Module2 . But then, how to retrieve instances?

var fooBindings = module2.Bindings.Where(b => b.Service.Equals(typeof(IFoo)));

// and then something like:
var instances = fooBindings.Select(b => kernel.Get(b));
// or kernel.Resolve(b) where b is instance of IBinding

// then: instances will contain only FooC, not the FooA and FooB.

I know that similar question has been asked here: Ninject GetAll Where Module = foo But is it not possible to leverage the Bindings member exposed by the module for that?

链接地址: http://www.djcxy.com/p/66280.html

上一篇: Erlang及其对堆内存的消耗

下一篇: 如何从模块中加载Ninject内核中的所有实例?