Skip to content
On this page

Try Getting an Optional Service by Service Type and Name

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

cs
[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));
}

snippet source | anchor