title: Autofac - resolving component with parameters dynamically
body: <p>I have a class that takes an interface as a constructor argument. There are two implementations of this interface and I want to decide what implementation to use at runtime based on a variable.</p>
  
  <p>The problem is that the class above is deep in an object heirarchy that is resolved by Autofac and so I can't pass in the argument.</p>
  
  <p>Somehing like below is what I am trying to achieve.</p>
  
  <pre><code>public interface IInterface1 {}
  public interface IInterface2 {}
  
  public class Class1 : IInterface2
  {
      public Class1(IInterface1 interface1)
      {
      }
  }
  
  public class Class2
  {
      public Class2(IInterface2 interface2)
      {
      }
  }
  
  public class Class3
  {
      public void GetClass2Instance(string interface1ImplementationToChoose)
      {
          // want to change which implementation of IInterface1 is resolved based on the interface1ImplementationToChoose variable
          var class2 = container.Resolve&lt;Class2&gt;();
      }
  }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>UPDATE:</p>
  
  <p>To clarify, this is an existing object hierarchy that is used by existing applications that work fine. Also, the object model is much larger than the one shown in this example. As a result I don't really want to have to pass down a factory to each constructor in the object graph to be used by a class deep in that graph.</p>
  
  <p>Is there a way of getting a different implementation of IInterface1 passed into Class1 without Class2 knowing anything about it?</p>
  
  <p>Thanks</p>