我想註冊同一類的多個實例,以便當我注入該類的枚舉時,我檢索所有實例。C#Unity依賴注入,我如何檢索一個可枚舉的實例?
public class ActionDialogType
{
public Type Type { get; set; }
public string Name { get; set; }
}
public class ActionDialogTypeUser
{
private IEnumerable<ActionDialogType> _types;
public ActionDialogTypeUser(IEnumerable<ActionDialogType> types)
{
_types = types
}
public void DoSomethingWithTypes()
{
// Do Something with types
}
}
到目前爲止,我已經有了:
public class UnityConfig
{
public IUnityContainer Register()
{
UnityContainer container = new UnityContainer();
ActionDialogType actionDialogType1 = new ActionDialogType
{
Name = "Something",
Type = typeof(Something)
};
container.RegisterInstance<IActionDialogType>(actionDialogType1, new ContainerControlledLifetimeManager());
ActionDialogType actionDialogType2 = new ActionDialogType
{
Name = "SomethingElse",
Type = typeof(SomethingElse)
};
container.RegisterInstance<ActionDialogType>(actionDialogType2, new ContainerControlledLifetimeManager());
container.RegisterType<IEnumerable<ActionDialogType>, ActionDialogType[]>();
return container;
}
}
誰能告訴我怎麼做呢?
似乎是一個奇怪的事情,但不會你只需要添加這些'ActionDialogType's到一個數組,該數組的'RegisterInstance'? – Crowcoder