c# - How to resolve list of dependencies in Autofac? -
i want register type, resolve type, , register instance using resolved values. this:
//register type: builder.registertype<validateimportmandatorycolumns>().named<ivalidateimport>("mandatorycolumn").as<ivalidateimport>(); builder.registertype<validateimportnonmandatorycolumns>().named<ivalidateimport>("nonmandatorycolumns").as<ivalidateimport>(); //resolve var t1 = container.resolvenamed<ivalidateimport>("mandatorycolumn"); var t2 = container.resolvenamed<ivalidateimport>("nonmandatorycolumns"); //create list resolved values: list<ivalidateimport> allvalidators = new list<ivalidateimport>(){t1,t2}; //register instance: builder.registerinstance(allvalidators).as<list<ivalidateimport>>();
this not working. can't resolve , register again. know how autofac? maybe approach wrong, please tell me if have better idea. goal inject list of validators different types use same interface.
autofac has built in support collection. if want resolve ivalidateimport
, can resolve ienumerable<ivalidateimport>
var allvalidators = container.resolve<ienumerable<ivalidateimport>>();
see implicit relationship types more information more information.
by way, if want update container, not required here, can use following piece of code.
var builder = new containerbuilder(); // registration var container = builder.build(); var updater = new containerbuilder(); // other registraitons // update container updater.update(container);
Comments
Post a Comment