Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try using named instances:</p> <pre> <code> IUnityContainer container = new UnityContainer(); container.RegisterType&lt;Type1&gt;(); container.RegisterType&lt;Type2&gt;("Instance 1", new ContainerControlledLifetimeManager()); container.RegisterType&lt;Type2&gt;("Instance 2", new ContainerControlledLifetimeManager()); container.RegisterType&lt;Type3&gt;(); Type1 type1 = container.Resolve&lt;Type1&gt;(); if (type1 == ...) { Type2 instance1 = container.Resolve&lt;Type2&gt;("Instance 1"); } else { Type2 instance2 = ontainer.Resolve&lt;Type2&gt;("Instance 2"); } </code> </pre> <p>You can do some checks on type 1 and decide which instance of type 2 you will need. Note that the "new ContainerControlledLifetimeManager()" parameter will initialize a singleton instance of the resisted type, so you will always get the same instance of type 2.</p> <p><strong>Update:</strong> Same with interfaces. Hope this helps.</p> <pre> <code> IUnityContainer container = new UnityContainer(); container.RegisterType&lt;TextDocument&gt;(); container.RegisterType&lt;ImageDocument&gt;(); container.RegisterType(typeof (IView), typeof (TextView), "Text", new ContainerControlledLifetimeManager()); container.RegisterType(typeof (IView), typeof (ImageView), "Image", new ContainerControlledLifetimeManager()); IDocument document = container.Resolve&lt;TextDocument&gt;(); IView view = null; if (document is TextDocument) { view = container.Resolve&lt;IView&gt;("Text"); } else { view = container.Resolve&lt;IView&gt;("Image"); } view.Show(); </code> </pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload