随着Spring.Net,它可以查询某(祖先)类型的所有对象。
var ctx = ContextRegistry.GetContext();
var setUsers = ctx.GetObjectsOfType(typeof(ISetUser)).Values.OfType<ISetUser>().ToList();
怎么可以这样用DryIoc做了什么?
随着Spring.Net,它可以查询某(祖先)类型的所有对象。
var ctx = ContextRegistry.GetContext();
var setUsers = ctx.GetObjectsOfType(typeof(ISetUser)).Values.OfType<ISetUser>().ToList();
怎么可以这样用DryIoc做了什么?
给定样本的类和接口的直接的答案是:
public interface IA { }
public interface IB { }
public class AB : IA, IB { }
public class AA : IA { }
[Test]
public void Resolve_all_services_implementing_the_interface()
{
var container = new Container();
container.Register<IB, AB>();
container.Register<AA>();
// resolve IA's, even if no IA service type was registered
var aas = container.GetServiceRegistrations()
.Where(r => typeof(IA).IsAssignableFrom(r.Factory.ImplementationType ?? r.ServiceType))
.Select(r => (IA)container.Resolve(r.ServiceType))
.ToList();
Assert.AreEqual(2, aas.Count);
}
如果你想找回一些接口,它可能是好的,从一开始(计划吧)注册它:
[Test]
public void Resolve_automatically_registered_interface_services()
{
var container = new Container();
// changed to RegisterMany to automatically register implemented interfaces as services
container.RegisterMany<AB>();
container.RegisterMany<AA>();
// simple resolve
var aas = container.Resolve<IList<IA>>();
Assert.AreEqual(2, aas.Count);
}