2

I have the following class:

public class Foo : IFoo
{
    public Foo(IBar[] bars);
}

IBar has multiple implementations that are all registered in the UnityContainer. When I call

UnityContainer.Resolve()
I want that the container to inject all known IBar implementations into the object.

How can I achieve this?

1 Answer 1

6

As long as your registered types have a name applied to them:

container.RegisterType<IBar, ActualBar>("ActualBar");
container.RegsiterType<IBar, YetAnotherBar>("YetAnotherBar");

And given a proper public constructor as you mention:

public Foo(IBar[] bars)
{
}

Unity will take care of resolving all named registered instances into your constructor out of the box.

Foo foo = container.Resolve<Foo>();
Sign up to request clarification or add additional context in comments.

2 Comments

Why does it need a name? I mean i expect it to work even without a name!
As long as each name is unique you will see all your instances or have access to them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.