Conditionally registers that an instance of implementationType will be
returned every time a serviceType is requested and where the supplied
predicate returns true. The instance is cached according to the supplied
lifestyle. The predicate will only be evaluated a finite number of times; the
predicate is unsuited for making decisions based on runtime conditions.
Namespace: SimpleInjector
Assembly: SimpleInjector (in SimpleInjector.dll) Version: 5.3.0
Syntax
public void RegisterConditional( Type serviceType, Type implementationType, Lifestyle lifestyle, Predicate<PredicateContext> predicate )
Parameters
- serviceType
- Type: SystemType
The base type or interface to register. This can be an open-generic type. - implementationType
- Type: SystemType
The actual type that will be returned when requested. - lifestyle
- Type: SimpleInjectorLifestyle
The lifestyle that defines how returned instances are cached. - predicate
- Type: SystemPredicatePredicateContext
The predicate that determines whether the implementationType can be applied for the requested service type. This predicate can be used to build a fallback mechanism where multiple registrations for the same service type are made. Note that the predicate will be called a finite number of times and its result will be cached for the lifetime of the container. It can't be used for selecting a type based on runtime conditions.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | Thrown when one of the arguments is a null reference. |
ArgumentException | Thrown when serviceType and implementationType are not a generic type or when serviceType is a partially-closed generic type. |
InvalidOperationException | Thrown when this container instance is locked and can not be altered. |
See Also