Skip to main content

Register Collection

VContainer supports implicit resolution of collection types.

Multiple registrations of the same interface can be resolved together with IEnumerable<T> or IReadOnlyList<T>.

builder.Register<IDisposable, A>(Lifetime.Scoped);
builder.Register<IDisposable, B>(Lifetime.Scoped);
class ClassA
{
public ClassA(IEnumerable<IDisposable> disposables) { /* ... */ }
}

OR

class ClassA
{
public ClassA(IReadOnlyList<IDisposable> disposables) { /* ... */ }
}
note

This is mainly used by internal functions such as the ITickable marker, etc.

Resolve Empty Collection

If you resolve a collection IEnumerable<T> or IReadOnlyList<T> but no T is registered, VContainer returns an empty collection. It does not throw an exception.

// No registrations for IService
class ClassA
{
public ClassA(IEnumerable<IService> services)
{
// services is empty (not null)
}
}