Perhaps I misunderstand, but RegisterSingle should work. You should be able to do this:
var types = ...
container.RegisterAll<IInterface>(types);
foreach (var type in types)
{
container.RegisterSingle(type, type);
}
UPDATE:
So what you are trying to do is to automate the following configuration:
// A, B, C and D implement both I1 and I2.
container.RegisterSingle<A>();
container.RegisterSingle<B>();
container.RegisterSingle<C>();
container.RegisterSingle<D>();
container.RegisterAll<I1>(typeof(A), typeof(B), typeof(C), typeof(D));
container.RegisterAll<I2>(typeof(A), typeof(B), typeof(C), typeof(D));
This would typically be the way to automate this. So do four steps:
- Find all types to register.
- Register the found types as singleton.
- Register the list of types as collection for
I1
. - Register the list of types as collection for
I2
.
This would look like this:
// using SimpleInjector.Extensions;
Type[] singletons = FindAllTypesToRegister();
foreach (Type type in singletons)
{
container.RegisterSingle(type, type);
}
container.RegisterAll(typeof(I1), singletons);
container.RegisterAll(typeof(I2), singletons);
However, since you are trying to split this into two steps and create one generic method that can handle each step, you will have to ignore when a concrete singleton type has already been registered. You can either do this by:
- Ignoring the registration by catching the exception thrown from
RegisterSingle
. - Override an existing registration by setting
container.Options.AllowOverridingRegistrations = true
before callingRegisterSingle
(disabling it afterwards would be safest).