Fork me on GitHub

Get all Services by Plugin Type Edit on GitHub


Please see Working with Enumerable Types for a lot more information about what's going on behind the scenes.

Once in a while you might want to get an enumerable of all the configured objects for a PluginType. That's done with the GetAllInstances() method shown below:


[Fact]
public void get_all_instances()
{
    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.GetAllInstances<IWidget>()
        .Select(x => x.GetType())
        .ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));

    // or

    container.GetAllInstances(typeof(IWidget))
        .OfType<IWidget>() // returns an IEnumerable, so I'm casting here
        .Select(x => x.GetType())
        .ShouldHaveTheSameElementsAs(typeof(AWidget), typeof(BWidget), typeof(CWidget));
}