title: Automatically Registering All Implementations of a Generic Interface
body: <p>My current registration code:</p>
  
  <pre><code>Assembly web = Assembly.Load("MyAssembly");
  types.AddRange(web.GetTypes());
  //etc.
  
  foreach (var theInterface in types.Where(t =&gt; t.IsInterface))
  {
      var assignableType = types.Where(t =&gt; theInterface.IsAssignableFrom(t) &amp;&amp; t != theInterface);
      foreach (var type in assignableType)
      {
          container.RegisterType(theInterface, type);
      }
  }
  </code></pre>
  
  <p>My generic interface and its implementations:</p>
  
  <pre><code>public interface IDomainEventHandler&lt;in T&gt;
  {
      void Handle(T message);
  }
  
  public class DomainEventHandler1 : IDomainEventHandler&lt;PhilTest1&gt;
  {
      public void Handle(PhilTest1 message)
      {
          throw new System.NotImplementedException();
      }
  }
  
  public class DomainEventHandler2 : IDomainEventHandler&lt;PhilTest2&gt;
  {
      public void Handle(PhilTest2 message)
      {
          throw new System.NotImplementedException();
      }
  }
  
  public class DomainEventHandler3 : IDomainEventHandler&lt;PhilTest2&gt;
  {
      public void Handle(PhilTest2 message)
      {
          throw new System.NotImplementedException();
      }
  }
  
  public class PhilTest1
  {
  
  }
  
  public class PhilTest2
  {
  
  }
  </code></pre>
  
  <p>Here is a simplified version of how I would resolve:</p>
  
  <pre><code>IEnumerable&lt;IDomainEventHandler&lt;PhilTest2&gt;&gt; listeners = Container.ResolveAll&lt;IDomainEventHandler&lt;PhilTest2&gt;&gt;();
  IEnumerable&lt;IDomainEventHandler&lt;PhilTest1&gt;&gt; listeners2 = Container.ResolveAll&lt;IDomainEventHandler&lt;PhilTest1&gt;&gt;();
  </code></pre>
  
  <p>This doesn't work--listeners and listeners2 are both empty after resolving.  This is not unexpected.</p>
  
  <p>In Windsor, I could do something like this:</p>
  
  <pre><code>container.Register(AllTypes.FromAssemblyNamed("MyAssembly").BasedOn(typeof(IDomainEventHandler&lt;&gt;)).WithService.Base());
  </code></pre>
  
  <p>How would I register all instances of IDomainEventHandler in Unity?  I'd prefer to keep the existing registration code intact (if possible).</p>