Fork me on GitHub

Get a Service by Plugin Type and Name Edit on GitHub


You can also request a named configuration for a given PluginType by using the overloads of IContainer.GetInstance() that take in a name like this:


[Fact]
public void get_a_named_instance()
{
    var container = new Container(x =>
    {
        x.For<IWidget>().Add<AWidget>().Named("A");
        x.For<IWidget>().Add<BWidget>().Named("B");
        x.For<IWidget>().Add<CWidget>().Named("C");
    });

    container.GetInstance<IWidget>("A").ShouldBeOfType<AWidget>();
    container.GetInstance<IWidget>("B").ShouldBeOfType<BWidget>();
    container.GetInstance<IWidget>("C").ShouldBeOfType<CWidget>();

    // or

    container.GetInstance(typeof(IWidget), "A").ShouldBeOfType<AWidget>();
    container.GetInstance(typeof(IWidget), "B").ShouldBeOfType<BWidget>();
    container.GetInstance(typeof(IWidget), "C").ShouldBeOfType<CWidget>();
}