title: Resolving by type name (or id, IComponentRegistration...) in Autofac
body: <p>I have <strong>multiple</strong> (generic) types registered with Autofac, all "implementing" a (generic) marker interface. Basically this is the registration and I can't change it (to be e.g. named):</p>
  
  <pre><code>builder.RegisterGeneric(typeof(MyType1&lt;&gt;)).As(typeof(IMarkerInterface&lt;&gt;));
  builder.RegisterGeneric(typeof(MyType2&lt;&gt;)).As(typeof(IMarkerInterface&lt;&gt;));
  builder.RegisterGeneric(typeof(MyType3&lt;&gt;)).As(typeof(IMarkerInterface&lt;&gt;));
  </code></pre>
  
  <p>where builder is a ContainerBuilder instance.
  <strong>Is there a proper way of resolving a type registered as IMarkerInterface&lt;> if I only know the implementation's (type) name (e.g. MyAssembly.MyType2)?</strong>
  The registration can simply be selected from IComponentContext.ComponentRegistry.Registrations with a LINQ-query, so in the end I also have the registration object, just can't activate it:</p>
  
  <ul>
  <li>registration.Activator.ActivateInstance() needs the constructor parameters, but I don't know these (although they can be injected by Autofac if resolved)</li>
  <li>There <a href="http://api.autofac.org/html/F19CFF59.htm" rel="nofollow">used to be a method</a> fully suitable for me (as I have the registration id and the ICompontentContext instance), but it seems that now (v2.5.2.830) it's gone.</li>
  <li>As the Service type of the type is the marker interface, I can't resolve by service type as it would resolve the one implementation that happens to be on the top.</li>
  </ul>
  
  <p>A workaround would be I think to mark the types with an interface derived from IMarkerInterface&lt;>, so they would all have unique services too. Another one would be to resolve IEnumerable>, but this would resolve all the implementations, not just the one that's needed (although this has a negligible impact on performance, it's still superfluous). But I don't know if this is the best solution, I feel there should be a more straightforward way.</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p><strong>Solution</strong></p>
  
  <p>Because I can't answer yet, here:
  Just mimicking the implementation Autofac.ResolutionExtensions once had, this code works (and I frankly don't know why: constructor params get properly filled; this should mean I've misunderstood what the "parameters" argument is).</p>
  
  <pre><code>registration.Activator.ActivateInstance(_componentContext, Enumerable.Empty&lt;Parameter&gt;());
  </code></pre>
  
  <p>where registration is the IComponentRegistration instance, corresponding to the implementation I wanted to instantiate.
  Apparently this also needs something to "initialize" the container with the current type params. A call to</p>
  
  <pre><code>_componentContext.IsRegistered&lt;IMarkerInterface&lt;TypeParameter&gt;&gt;()
  </code></pre>
  
  <p>is enough.</p>
  
  <p>Thanks for Sebastien Weber and kvalcanti for the inputs!</p>