title: Is it possible to use Ninject Factory Extensions' ToFactory method with open generics?
body: <p>
  I'm building on a previously answered <a href="https://stackoverflow.com/q/15868820/533958">question</a> in which <code>ICar</code> implementations are bound using <a href="https://github.com/ninject/ninject.extensions.conventions/wiki" rel="nofollow noreferrer">Ninject Conventions Extensions</a> and a custom <a href="https://github.com/ninject/ninject.extensions.conventions/wiki/Projecting-Services-to-Bind#custom-binding-generators" rel="nofollow noreferrer">IBindingGenerator</a>, and the <code>ICarFactory</code> interface is bound using the <a href="https://github.com/ninject/ninject.extensions.factory/wiki" rel="nofollow noreferrer">Ninject Factory Extensions'</a> <code>ToFactory()</code> method and a <a href="https://github.com/ninject/ninject.extensions.factory/wiki/Factory-interface%3A-custom-instance-providers" rel="nofollow noreferrer">custom instance provider</a>.</p>
  
  <p>I'm trying to refactor so that I can bind and make use of a <code>IVehicleFactory&lt;T&gt;</code>, where <code>T</code> is constrained to <code>ICar</code>, rather than the previous <code>ICarFactory</code>. This way, I can specify the vehicle I want in the generic type parameter, instead of passing in the name of the vehicle type in the factory's <code>CreateCar()</code> method.</p>
  
  <p><strong>Is it possible to bind open generic interfaces using the <code>ToFactory()</code> technique?</strong></p>
  
  <p>I have a  feeling that I'm barking up the wrong tree, but when I was specifying an <code>ICar</code> type by its name, it seemed like the natural evolution to specify the <code>ICar</code> type itself as a generic type parameter...</p>
  
  <p>Here's the test that currently fails:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[Fact]
  public void A_Generic_Vehicle_Factory_Creates_A_Car_Whose_Type_Equals_Factory_Method_Generic_Type_Argument()
  {
      using (StandardKernel kernel = new StandardKernel())
      {
          // arrange
          kernel.Bind(typeof(IVehicleFactory&lt;&gt;))
              .ToFactory(() =&gt; new UseFirstGenericTypeArgumentInstanceProvider());
  
          kernel.Bind(
              scanner =&gt; scanner
                             .FromThisAssembly()
                             .SelectAllClasses()
                             .InheritedFrom&lt;ICar&gt;()
                             .BindWith(new BaseTypeBindingGenerator&lt;ICar&gt;()));
          IVehicleFactory&lt;Mercedes&gt; factory 
              = kernel.Get&lt;IVehicleFactory&lt;Mercedes&gt;&gt;();
  
          // act
          var car = factory.CreateVehicle();
  
          // assert
          Assert.IsType&lt;Mercedes&gt;(car);
      }
  }
  </code></pre>
  
  <p>And the <code>InvalidCastException</code> thrown:</p>
  
  <pre class="lang-cs prettyprint-override"><code>System.InvalidCastException was unhandled by user code
    Message=Unable to cast object of type 'Castle.Proxies.ObjectProxy' to type 'IVehicleFactory`1[Mercedes]'.
    Source=System.Core
    StackTrace:
         at System.Linq.Enumerable.&lt;CastIterator&gt;d__b1`1.MoveNext()
         at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
         at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 37
         at NinjectFactoryTests.A_Generic_Vehicle_Factory_Creates_A_Car_Whose_Type_Name_Equals_Factory_Method_String_Argument() in C:\Programming\Ninject.Extensions.Conventions.Tests\NinjectFactoryTests.cs:line 37
    InnerException: 
  </code></pre>
  
  <p>And the factory interface:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public interface IVehicleFactory&lt;T&gt; where T : ICar
  {
      T CreateVehicle();
  }
  </code></pre>
  
  <p>And the custom instance provider, whose breakpoints I can't even get the debugger to stop on, so I really don't know what's going on in there:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class UseFirstGenericTypeArgumentInstanceProvider : StandardInstanceProvider
  {
      protected override string GetName(MethodInfo methodInfo, object[] arguments)
      {
          var genericTypeArguments = methodInfo.GetGenericArguments();
          var genericMethodDefinition = methodInfo.GetGenericMethodDefinition();
          var g = genericMethodDefinition.MakeGenericMethod(genericTypeArguments.First());
          return g.MemberType.GetType().Name;
      }
  
      protected override ConstructorArgument[] GetConstructorArguments(MethodInfo methodInfo, object[] arguments)
      {
          return base.GetConstructorArguments(methodInfo, arguments).Skip(1).ToArray();
      }
  }
  </code></pre>
  
  <p><strong>EDIT 1 - Change</strong> <code>IVehicleFactory</code> <strong>signature and custom instance provider</strong></p>
  
  <p>Here's I've changed the <code>IVehicleFactory</code> signature to use a generic <code>Create&lt;T&gt;()</code> method, and explicitly bound <code>Mercedes</code> to itself.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public interface IVehicleFactory
  {
      T CreateVehicle&lt;T&gt;() where T : ICar;
  }
  </code></pre>
  
  <p>And the new custom instance provider which returns the name of the first generic type parameter:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class UseFirstGenericTypeArgumentInstanceProvider : StandardInstanceProvider
  {
      protected override string GetName(MethodInfo methodInfo, object[] arguments)
      {
          var genericTypeArguments = methodInfo.GetGenericArguments();
          return genericTypeArguments[0].Name;
      }
  }
  </code></pre>
  
  <p>Here's the new test, still not passing:</p>
  
  <pre class="lang-cs prettyprint-override"><code>[Fact]
  public void A_Generic_Vehicle_Factory_Creates_A_Car_Whose_Type_Name_Equals_Factory_Method_String_Argument()
  {
      using (StandardKernel kernel = new StandardKernel())
      {
          // arrange
          kernel.Bind&lt;IVehicleFactory&gt;()
              .ToFactory(() =&gt; new UseFirstGenericTypeArgumentInstanceProvider())
              .InSingletonScope();
          kernel.Bind&lt;Mercedes&gt;().ToSelf();
          IVehicleFactory factory = kernel.Get&lt;IVehicleFactory&gt;();
  
          // act
          var car = factory.CreateVehicle&lt;Mercedes&gt;();
  
          // assert
          Assert.IsType&lt;Mercedes&gt;(car);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>A <code>Ninject.ActivationException</code> is thrown:</p>
  
  <pre class="lang-cs prettyprint-override"><code>Ninject.ActivationException: Error activating Mercedes
  No matching bindings are available, and the type is not self-bindable.
  Activation path:
    1) Request for Mercedes
  </code></pre>
  
  <p>I don't know why it can't find the <code>Mercedes</code> class, since I explicitly self-bound it. Can you spot what I'm doing wrong?</p>