title: Autofac: Resolve a specific dependency to a named instance
body: <p>Using Autofac, I would like to register a component and specify for a specific dependency to be resolved to a named instance.</p>
  
  <p>I found samples like the following using constructor injection which is almost what I want.</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>builder.Register(c =&gt; new ObjectContainer(ConnectionStrings.CustomerDB))
      .As&lt;IObjectContainer&gt;()
      .Named("CustomerObjectContainer");
  
  builder.Register(c =&gt; new ObjectContainer(ConnectionStrings.FooDB))
      .As&lt;IObjectContainer&gt;()
      .Named("FooObjectContainer");
  
  builder.Register(c =&gt; new CustomerRepository(
      c.Resolve&lt;IObjectContainer&gt;("CustomerObjectContainer"));
  
  builder.Register(c =&gt; new FooRepository(
      c.Resolve&lt;IObjectContainer&gt;("FooObjectContainer"));
  </code></pre>
  
  <p>However, I need this with property injection and I don't want to specify all dependencies.</p>
  
  <p>Something like:</p>
  
  <pre class="lang-cs prettyprint-override"><code>builder.Register&lt;CustomerRepository&gt;().With&lt;IObjectContainer&gt;("CustomerObjectContainer");    
  builder.Register&lt;FooRepository&gt;().With&lt;IObjectContainer&gt;("FooObjectContainer");
  </code></pre>
  
  <p>The build up of all unspecified dependnecies should happen with unnamed instances.</p>
  
  <p>Thanks,
  Alex</p>
  
  <p>[ADDITION TO THE ANSWER FROM Danielg]</p>
  
  <p>An overload to resolve by type for any property of that type.</p>
  
  <pre class="lang-cs prettyprint-override"><code>    public static IRegistrationBuilder&lt;TLimit, TReflectionActivatorData, TStyle&gt; WithDependency&lt;TLimit, TReflectionActivatorData, TStyle, TProperty&gt;(
          this IRegistrationBuilder&lt;TLimit, TReflectionActivatorData, TStyle&gt; registration,
          Func&lt;IComponentContext, TProperty&gt; valueProvider)
          where TReflectionActivatorData : ReflectionActivatorData
      {
          return registration.WithProperty(new ResolvedParameter((p, c) =&gt;
              {
                  PropertyInfo prop;
                  return p.TryGetDeclaringProperty(out prop) &amp;&amp;
                      prop.PropertyType == typeof(TProperty);
              },
              (p, c) =&gt; valueProvider(c)));
      }
  </code></pre>