Fork me on GitHub

Try Geting an Optional Service by Plugin Type and Name Edit on GitHub


Just use the IContainer.TryGetInstance<T>(name) or IContainer.TryGetInstance(Type pluginType, string name) method as shown below:


[Fact]
public void TryGetInstanceViaNameAndGeneric_ReturnsInstance_WhenTypeFound()
{
    addColorInstance("Red");
    addColorInstance("Orange");
    addColorInstance("Blue");

    // "Orange" exists, so an object should be returned
    var instance = _container.TryGetInstance<Rule>("Orange");
    instance.ShouldBeOfType(typeof(ColorRule));
}