.net - How can I make Unity resolve a type to a property of another type? -
imagine have following classes:
class { public b b { get; set; } } class b { }
i have unity container a
registered singleton:
container = new unitycontainer(); container.registertype<a>(new containercontrolledlifetimemanager());
i want use container resolve b
:
var b = container.resolve<b>();
result
a new instance of b
in b
variable.
desired result
the value of registered a
's b
property should returned.
how can achieve this?
additional information
in specific situation, a
linq sql datacontext
, , b
1 of tables.
you can use injectionfactory returns a.b
when instance of b requested/required:
iunitycontainer container = new unitycontainer(); container.registertype<a>(new containercontrolledlifetimemanager()); container.registertype<b>(new injectionfactory(c => c.resolve<a>().b)); var = container.resolve<a>(); // a.b set somewhere a.b = new b(); var b = container.resolve<b>(); system.diagnostics.debug.assert(referenceequals(a.b, b));
Comments
Post a Comment