c# - Autofac two properties with the same type -
if i've got 2 properties in class same interface type , want inject 2 different conrete types each how do autofac either property or constructor injection.
eg.
class : ia { public ib propertyb { get; set; } public ib propertyc { get; set; } public a(ib b, ib c) { propertyb = b; propertyc = c; } public void printb() { propertyb.print(); } public void printc() { propertyc.print(); } }
i've tried of course c inject both properties
var builder = new containerbuilder(); builder.registertype<b>().as<ib>(); builder.registertype<c>().as<ib>(); builder.registertype<a>().as<ia>(); var container = builder.build(); var = container.resolve<ia>();
or same result:
builder.registertype<b>().as<ib>(); builder.registertype<c>().as<ib>(); builder.registertype<a>().as<ia>().propertiesautowired(); var container = builder.build(); var = container.resolve<ia>();
is there way can tell autofac want b in propertyb , c in propertyc ?
using property injection, can following:
builder.registertype<a>() .as<ia>() .onactivating(e => { e.instance.propertyb = e.context.resolve<bimpl1>(); e.instance.propertyc = e.context.resolve<bimpl2>(); });
Comments
Post a Comment