title: Ninject - binding generic types with constraints
body: <p>Is it possible to set up a ninject binding to respect a generic constraint?</p>
  
  <p>For example:</p>
  
  <pre class="lang-cs prettyprint-override"><code>interface IFoo { }
  interface IBar { }
  interface IRepository&lt;T&gt; { }
  
  class FooRepository&lt;T&gt; : IRepository&lt;T&gt; where T : IFoo { }
  class BarRepository&lt;T&gt; : IRepository&lt;T&gt; where T : IBar { }
  
  class Foo : IFoo { }
  class Bar : IBar { }
  
  class Program
  {
      static void Main(string[] args)
      {
          IKernel kernel = new StandardKernel();
  
          // Use this binding only where T : IFoo
          kernel.Bind(typeof(IRepository&lt;&gt;)).To(typeof(FooRepository&lt;&gt;));
  
          // Use this one only where T : IBar
          kernel.Bind(typeof(IRepository&lt;&gt;)).To(typeof(BarRepository&lt;&gt;));
  
          var fooRepository = kernel.Get&lt;IRepository&lt;Foo&gt;&gt;();
          var barRepository = kernel.Get&lt;IRepository&lt;Bar&gt;&gt;();
      }
  }
  </code></pre>
  
  <p>Calling this code as-is will produce a multiple activation paths exception:-</p>
  
  <blockquote>
    <p>Error activating IRepository{Foo}: More than one matching bindings are available.</p>
  </blockquote>
  
  <p>How can I set up the bindings to be conditional on the value of T? Ideally I'd like them to pick up the constraints from the target type, as I've already defined them there, but if I have to define them again in the binding that's also acceptable.</p>