我在为我的 Castle Windsor 容器设置正确的绑定方面有点挣扎。最初的问题涉及 log4net 和命名记录器,可以在这个问题中找到描述:设置 log4net 记录器的名称。
但是,现在问题缩小到了这一点:
我有一个实现 IAuditor 的类 Log4NetAuditor。我的一些类扩展了我的抽象类 AuditableComponent,它具有 IAuditor 类型的属性 Auditor。当我用 Castle 注册 IAuditor 时,它知道如何为所有扩展 AuditableComponent 的类解析和设置 Auditor。
现在,我需要 Log4NetAuditor 的构造函数来获取 Type 参数。该类型应该是扩展 AuditableComponent 的类的类型。我试图通过工厂方法实现这一点,但我无法解决:
container.Register(Component.For<IAuditor>().ImplementedBy<Log4NetAuditor>()
.UsingFactoryMethod(RegisterAuditor).LifeStyle.Transient);
private static IAuditor RegisterAuditor(IKernel kernel, ComponentModel model, CreationContext context)
{
Type loggerType = //magic to find correct type happens here
return new Log4NetAuditor(loggerType, kernel.Resolve<ILoggerFactory>());
}
我对这个问题视而不见。任何人都可以帮忙吗?