Search code examples
c#genericscastle-windsorsimple-injectoropen-generics

Castle Windsor registering open generics ILogger


I have Simple Injector registration of my:

container.RegisterConditional(typeof(ILogManager),
c => typeof(LogManager<>).MakeGenericType(c.Consumer.ImplementationType),
Lifestyle.Singleton,
c => true);

I need to register same LogManager in different project using Castle Windsor.

I have tried

container.Register(Component.For(typeof(ILogger))
            .ImplementedBy(typeof(Log4NetLogger<>).MakeGenericType())
            .LifeStyle.Singleton.Start());

Cannot make it working.


Solution

  • Unfortunately this is more complicated with Castle. But you can achieve the same result with SubDependencyResolver:

    public class LoggerResolver : ISubDependencyResolver
    {
        public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
        {
            return dependency.TargetType == typeof(ILogger);
        }
    
        public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
        {
            var logger = typeof(LogManager<>).MakeGenericType(model.Implementation);
            return Activator.CreateInstance(logger);
        }
    }
    

    than add it into the kernel:

    Kernel.Resolver.AddSubResolver(new LoggerResolver())
    

    Another way is to use GenericImplementationMatchingStrategy. This one will work also in situation if LogManager has some dependencies:

    public class OpenGenericAncestorMatchingStrategy : IGenericImplementationMatchingStrategy
    {
        public Type[] GetGenericArguments(ComponentModel model, CreationContext context)
        {
            return new[] { context.Handler.ComponentModel.Implementation };
        }
    }
    

    and registration:

    container.Register(Component.For<ILogger>().ImplementedBy(typeof(LogManager<>), new OpenGenericAncestorMatchingStrategy()));