Castle Windsor 注册开放泛型 ILogger
Castle Windsor registering open generics ILogger
我有我的 Simple Injector 注册:
container.RegisterConditional(typeof(ILogManager),
c => typeof(LogManager<>).MakeGenericType(c.Consumer.ImplementationType),
Lifestyle.Singleton,
c => true);
我需要在使用 Castle Windsor 的不同项目中注册相同的 LogManager。
我试过了
container.Register(Component.For(typeof(ILogger))
.ImplementedBy(typeof(Log4NetLogger<>).MakeGenericType())
.LifeStyle.Singleton.Start());
无法正常工作。
不幸的是,这在 Castle 中更为复杂。但是您可以使用 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);
}
}
比加入内核:
Kernel.Resolver.AddSubResolver(new LoggerResolver())
另一种方法是使用 GenericImplementationMatchingStrategy。如果 LogManager 有一些依赖关系,这个也可以工作:
public class OpenGenericAncestorMatchingStrategy : IGenericImplementationMatchingStrategy
{
public Type[] GetGenericArguments(ComponentModel model, CreationContext context)
{
return new[] { context.Handler.ComponentModel.Implementation };
}
}
并注册:
container.Register(Component.For<ILogger>().ImplementedBy(typeof(LogManager<>), new OpenGenericAncestorMatchingStrategy()));
我有我的 Simple Injector 注册:
container.RegisterConditional(typeof(ILogManager),
c => typeof(LogManager<>).MakeGenericType(c.Consumer.ImplementationType),
Lifestyle.Singleton,
c => true);
我需要在使用 Castle Windsor 的不同项目中注册相同的 LogManager。
我试过了
container.Register(Component.For(typeof(ILogger))
.ImplementedBy(typeof(Log4NetLogger<>).MakeGenericType())
.LifeStyle.Singleton.Start());
无法正常工作。
不幸的是,这在 Castle 中更为复杂。但是您可以使用 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);
}
}
比加入内核:
Kernel.Resolver.AddSubResolver(new LoggerResolver())
另一种方法是使用 GenericImplementationMatchingStrategy。如果 LogManager 有一些依赖关系,这个也可以工作:
public class OpenGenericAncestorMatchingStrategy : IGenericImplementationMatchingStrategy
{
public Type[] GetGenericArguments(ComponentModel model, CreationContext context)
{
return new[] { context.Handler.ComponentModel.Implementation };
}
}
并注册:
container.Register(Component.For<ILogger>().ImplementedBy(typeof(LogManager<>), new OpenGenericAncestorMatchingStrategy()));