开发者

Castle Windsor: Can I get all instances of a type?

开发者 https://www.devze.com 2023-02-20 03:47 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消