title: Generic Factory with type parameter
body: <p>I have the following situation.</p>
  
  <p>My Factory class needs to create appropriate Strategy objects based on the input string argument to the CreateStrategy function.</p>
  
  <p>Strategy1, Strategy2 etc are all derived from a common StrategyBase class. However each strategy has a different Validation mechanism which is the type parameter to the Factory class. However, the StrategyValidators are not of any common type and have different interfaces.</p>
  
  <p>Therefore, in the below code, I am unable to specify any common constraint on the StrategyValidator type.</p>
  
  <p>I am new to C# and hence not sure if there exists any mechanism to get over this design issue. Please suggest</p>
  
  <pre><code>public class Factory
  {
      //Create the appropriate Concrete Implementation class based on the type
      public static StrategyBase CreateStrategy&lt;StrategyValidator&gt;(String Type)
      {
          StrategyBase EnumImp = null;
  
          // WMI based implementation
          if (Type == "Type1")
          {
              s = Strategy1&lt;StrategyValidator&gt;.Instance;
          }
          else if (Type = "Type2")
          {
              s = Strategy2&lt;StrategyValidator&gt;.Instance;
          }
          return s;
      }
  
      private StrategyBase s;
  }
  </code></pre>
  
  <p>Here's the intended usage</p>
  
  <pre><code>Factory f = new Factory(); 
  
  f.CreateStrategy&lt;WMIValidator&gt;("WMI");
  f.CreateStrategy&lt;ABCDValidator&gt;("ABCD");
  </code></pre>
  
  <p>where <code>WMIValidator</code> and <code>ABCDValidator</code> are unrelated types, but the actual classes created by <code>CreateStrategy</code> function are related in a hierarchy e.g. having a common base <code>StrategyBase</code></p>
  
  <p>Here is a sample code to illustrate the issue</p>
  
  <pre><code>namespace TestCSharp
  {
      public interface IStrategy
      {
      };
  
      public interface S1 : IStrategy
      {
          void f1();
          void f2();
      };
  
      public class S1Concrete : S1
      {
          public void f1() { }
          public void f2() { }
      }
  
      public interface S2 : IStrategy
      {
          void f3();
          void f4();
      };
  
      public class S2Concrete : S2
      {
          public void f3() { }
          public void f4() { }
      };
  
      public interface ProductBase
      {
      };
  
      class Product1&lt;T&gt; : ProductBase where T : S1
      {
      };
  
      class Product2&lt;T&gt; : ProductBase where T : S2
      {
      };
  
      public class Factory
      {
          public ProductBase Create&lt;T&gt;(String Type)
          {
              if (Type == "P1")
                  return new Product1&lt;T&gt;();
              else if (Type == "P2")
                  return new Product2&lt;T&gt;();
          }
      };
  
      class Program
      {
          static void Main(string[] args)
          {
              Factory f = new Factory();
              ProductBase s = f.Create&lt;S1Concrete&gt;("Type1");
          }
      }
  }
  </code></pre>
  
  <p>The error I get is </p>
  
  <blockquote>
    <p>The type 'T' cannot be used as type parameter 'T' in the generic type
    or method 'TestCSharp.Product1'. There is no boxing conversion or
    type parameter conversion from 'T' to 'TestCSharp.S1'.</p>
  </blockquote>