使用与父类型相同的类型参数解析Unity中的开放泛型类型

时间:2014-05-09 14:59:41

标签: c# generics dependency-injection unity-container

我有一个抽象的泛型类LogWriter<T>。我有一个名为MultiClientLogWriter<T>的实现,它包含多个LogWriter<T>实例并写入所有实例。我想让一个MutliClientLogWriter包含几个不同类型的编写器默认。我有这个代码,但它不起作用,因为(当然)你无法解析开放的泛型类型:

unityContainer.RegisterType(typeof (LogWriter<>), typeof (TraceLogWriter<>), "trace", new PerRequestLifetimeManager(),
  new InjectionConstructor(Severity.Debug));

unityContainer.RegisterType(typeof (LogWriter<>), typeof (LogSinkWriter<>), "sink",
  new PerRequestLifetimeManager(), new InjectionConstructor(Severity.Debug, logSink));

unityContainer.RegisterType(typeof (LogWriter<>), typeof (MultiClientLogWriter<>),
   new PerRequestLifetimeManager(),
   new InjectionConstructor(unityContainer.Resolve(typeof (LogWriter<>), "sink"),
   unityContainer.Resolve(typeof (LogWriter<>), "trace")));

但是,我希望这个链接下去(也就是说,如果请求MultiClientLogWriter<Controller>我想要一个LogSinkWriter<Controller>等等),所以你认为会有一些方法实现这一目标。

0 个答案:

没有答案