Castle Windsor: Can I get all instances of a type?
I am registering a bunch of ITask implementations with Windsor using N开发者_JAVA百科amed to separate them. Is there a way a class can request all instances of ITask?
Windsor can do what you request via the CollectionResolver
subdependency resolver:
var container = new WindsorContainer();
var kernel = container.Kernel;
kernel.Resolver.AddSubResolver(new CollectionResolver(kernel));
Now, if you register multiple implementations of ITask
, your task runner can have a ctor like this:
public TaskRunner(IEnumerable<ITask> tasks)
{
// ...
}
which is what you want, right?
Yes,
container.ResolveAll<ITask>()
This blog post explains in detail how to do it in a container ignorant way:
10 Advanced Windsor Tricks – 3. How to resolve arrays
Similar to the other answer given.
精彩评论