From d110cae5ab62e87c8c29b5808ad7513fe6818c38 Mon Sep 17 00:00:00 2001 From: Travis Illig Date: Wed, 14 Jun 2023 17:28:07 -0700 Subject: [PATCH] Note about open generics and IfNotRegistered. --- docs/register/registration.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/register/registration.rst b/docs/register/registration.rst index d85497f..e8f65f9 100644 --- a/docs/register/registration.rst +++ b/docs/register/registration.rst @@ -487,6 +487,15 @@ These extensions run at the time of ``ContainerBuilder.Build()`` and will execut reg.IsRegistered(new TypedService(typeof(IService))) && reg.IsRegistered(new TypedService(typeof(HandlerB)))); + // Be careful with open generics - IfNotRegistered and IsRegistered + // only work with CLOSED generics because that's what you'd resolve! + // Note the check is for a closed generic that would be registered + // as part of the open generic - if you put an open generic in the + // IfNotRegistered check, it'll always appear not registered. + builder.RegisterGeneric(typeof(CommandHandler<>)) + .As(typeof(ICommandHandler<>)) + .IfNotRegistered(typeof(ICommandHandler)); + // This is when the conditionals actually run. Again, // they run in the order the registrations were added // to the ContainerBuilder.