是否可以向统一容器注册以下递归结构:
public interface IFoo
{
IBar[] Bars { get; set; }
}
public interface IBar
{
IFoo[] Foos { get; set; }
}
假设每个接口存在多个命名实例:
public class Foo1 : IFoo
{
public IBar[] Bars { get; set; }
}
public class Foo2 : IFoo
{
public IBar[] Bars { get; set; }
}
public class Bar1 : IBar
{
public IFoo[] Foos { get; set; }
}
public class Bar2 : IBar
{
public IFoo[] Foos { get; set; }
}
和注册:
var container = new UnityContainer();
container.RegisterType<IFoo, Foo1>("foo1");
container.RegisterType<IFoo, Foo2>("foo2");
container.RegisterType<IBar, Bar1>("bar1");
container.RegisterType<IBar, Bar2>("bar2");
var instanceOfBar = container.Resolve<IBar>("bar1");
如何配置 Unity 容器以便自动注入集合属性?