title: Error: "The type cannot be used as type parameter in the generic type of method." Can this be solved with constraints or casting?
body: <p>I ran into a compilation error while working on an experiment with factories. This is not an actual factory implementation I would use, as I would probably use a DI system in real code. I wanted to state this up front since that's not what I'm looking for help with.</p>
  
  <p>What is confusing me with the snippet below is how to constrain the Create() method so that multiple types can be returned. The compiler error is:</p>
  
  <pre><code>The type 'TConcreteType' cannot be used as type parameter 'TConcreteType'
  in the generic type or method 'Tests.IFactory&lt;TFactoryType&gt;.Create&lt;TConcreteType&gt;(int)'.
  There is no boxing conversion or type parameter conversion from 'TConcreteType' to 
  'Tests.IFoo'.
  </code></pre>
  
  <p>Here is the important part of the sample code:</p>
  
  <pre><code>public static class UniversalFactory
  {
      // New factories should be added here.
      private static readonly IFactory&lt;IFoo&gt; FooFactoryEx;
      private static readonly IFactory&lt;IBar&gt; BarFactoryEx;
  
      static UniversalFactory()
      {
          // These bindings could also be provided via a DI framework like Ninject.
          FooFactoryEx = new FooFactory();
          BarFactoryEx = new BarFactory();
      }
  
      // Maps concrete objects to factories according to the interface(s) they implement.
      public static TConcreteType Create&lt;TConcreteType&gt;(int id)
      {
          if (typeof(TConcreteType).IsAssignableFrom(typeof(IFoo)))
              return FooFactoryEx.Create&lt;TConcreteType&gt;(id);
          if (typeof(TConcreteType).IsAssignableFrom(typeof(IBar)))
              return BarFactoryEx.Create&lt;TConcreteType&gt;(id);
          return default(TConcreteType);
      }
  }
  </code></pre>
  
  <p>The test code for the above looks like:</p>
  
  <pre><code>public class TestClass
  {
      public void TestMain()
      {
          var foo1 = UniversalFactory.Create&lt;FooImpl&gt;(1);
          var foo2 = UniversalFactory.Create&lt;FooImpl&gt;(2);
          var bar1 = UniversalFactory.Create&lt;BarImpl&gt;(1);
      }
  }
  </code></pre>
  
  <p>The error seems to be that TConcreteType has to be constrained to IFoo for this line:</p>
  
  <pre><code>return FooFactoryEx.Create&lt;TConcreteType&gt;(id);
  </code></pre>
  
  <p>While simultaneously (impossibly) constrained to IBar for this line:</p>
  
  <pre><code>return BarFactoryEx.Create&lt;TConcreteType&gt;(id);
  </code></pre>
  
  <p>However I would have expected the this to work without constraint since I am able to do type checking such as:</p>
  
  <pre><code>if (typeof(TConcreteType).IsAssignableFrom(typeof(IFoo)))
  </code></pre>
  
  <p>However, no amount of constraint or type casting resolved the situation. Am I trying to do something impossible, or is there an obvious constraint implied from the compiler error that I have not tried yet?</p>
  
  <p><strong>EDIT #1:</strong> I should point out that "FooImpl : IFoo" and "BarImpl : IBar".</p>
  
  <p><strong>EDIT #2:</strong> This info was added for Chris Martin since I left out a crucial type constraint in my example:</p>
  
  <pre><code>public interface IFactory&lt;in TFactoryType&gt;
  {
      TConcreteType Create&lt;TConcreteType&gt;(int id) where TConcreteType : TFactoryType;
  }
  </code></pre>