当请求 a 时,您需要告诉 Unity 您想要什么List<IWorkflowModule>
。因此,您可以执行以下操作:
IUnityContainer container = new UnityContainer();
// Default registration if required
container.RegisterType<IWorkflowModule, StartView>();
// IEnumerable Registration
container.RegisterType<IWorkflowModule, StartView>("WorkflowConfigReaderItem1");
container.RegisterType<IWorkflowModule, EndView>("WorkflowConfigReaderItem2");
container.RegisterType<List<IWorkflowModule>>(new InjectionFactory(c =>
{
return c.ResolveAll<IWorkflowModule>().ToList();
}));
Unity 确实知道如何解析数组,因此如果您要使用数组,IWorkflowModule
则不必注册 InjectionFactory:
public WorkflowConfigReader(Lazy<IWorkflowModule[]>
availableWorkflowModules)
{
this.availableWorkflowModules = availableWorkflowModules;
}
// Default registration if required
container.RegisterType<IWorkflowModule, StartView>();
container.RegisterType<IWorkflowModule, StartView>("WorkflowConfigReaderItem1");
container.RegisterType<IWorkflowModule, EndView>("WorkflowConfigReaderItem2");
var configReader = container.Resolve<WorkflowConfigReader>();