c# - Unity How to access property on ResolvedParameter<T> and inject into RegisterType -
i trying register type takes 2 strings parameter in it's constructor. strings properties off of 2 other registered types. have tried following, parameters empty when idatabasemanagement injected though other injection types resolve , have values properties:
container.registertype<iconnectioninfo, connectioninfo>() .registertype<iuserinfo, userinfo>() .registertype<idatabasemanagement, databasemanagement>(new injectionconstructor( container.resolve<iconnectioninfo>().connection , container.resolve<iuserinfo>().currentuser.useremployeenumber )); i have tried casting resolvedparameter, blows on casting:
container.registertype<iconnectioninfo, connectioninfo>() .registertype<iuserinfo, userinfo>() .registertype<idatabasemanagement, databasemanagement>(new injectionconstructor( ((iconnectioninfo)(new resolvedparameter<iconnectioninfo>())).connection , ((iuserinfo)(new resolvedparameter<iuserinfo>())).currentuser.useremployeenumber )); any appreciated. thanks!
the cleanest answer change databasemanagement constructor take in iconnectioninfo , iuserinfo instead of 2 strings.
if not acceptable, can use injectionfactory instead.
container.registertype<idatabasemanagement, databasemanagement>( new injectionfactory(c => { var connectioninfo = c.resolve<iconnectioninfo>(); var userinfo = c.resolve<iuserinfo>(); return new databasemanagement( connectioninfo.connection, userinfo.currentuser.useremployeenumber); })); the reason failing in first example becuase resolving instance of iconnectioninfo , iuserinfo upon executing registertype call , using single instances resolves of idatabasemanagement. injectionfactory executed every time resolve called.
Comments
Post a Comment