.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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -