title: c# Factory for concrete implementation of generic base class
body: <p>I have a Base class which is generic.
  I have a concrete class which implements the base class.</p>
  
  <p>How would I create a factory class/method for delivering different types of concrete classes?</p>
  
  <p>Here an example:</p>
  
  <pre><code>public class ReceiverBase&lt;T&gt;
      where T : IInterpreter
  { ... }
  
  public class SpecialReceiver : ReceiverBase&lt;OwnInterpreter&gt; { ... }
  
  public class ReceiverFactory&lt;T&gt;
      where T : ReceiverBase&lt;IInterpreter&gt;, new()
  
      public T Create(string type) {
          switch(type) {
              default:
                  return new SpecialReceiver();
          }
      }
  }
  </code></pre>
  
  <p>The problem is that ReceiverBase seems not to be possible because the compiler only wants classes as Constraints, not interfaces.
  And the second problem is that I cannot convert SpecialReceiver to T.</p>
  
  <p>So is there a way to get this working?</p>
  
  <p>=== EDIT: Added example according to first answer ===</p>
  
  <pre><code>public interface IInterpreter
  {
  }
  
  public class OwnInterpreter : IInterpreter
  {
      public void Dispose()
      {
          throw new NotImplementedException();
      }
  
      public void DoSomething() { }
  }
  
  
  
  public abstract class ReceiverBase&lt;T&gt;
      where T : IInterpreter
  {
      public T MyReceiver { get; set; }
  
      internal abstract void Start();
  }
  
  public class SpecialReceiver&lt;T&gt; : ReceiverBase&lt;T&gt;
      where T : IInterpreter, new()
  {
      public void CheckSomething()
      {
          MyReceiver.DoSomething();
      }
  
      internal override void Start()
      {
          MyReceiver = new T();
      }
  }
  
  public class ReceiverFactory&lt;T&gt;
      where T : IInterpreter, new()
  {
      public static ReceiverBase&lt;T&gt; Create(string type)
      {
          switch (type)
          {
              default:
                  return new SpecialReceiver&lt;T&gt;();
          }
      }
  }
  </code></pre>
  
  <p>The Problem is: MyReceiver.DoSomething(); will not work.
  Additionally I would have to call the factory like this: <code>ReceiverFactory&lt;OwnInterpreter&gt;.Create("");</code> I'd like to have it that way: <code>ReceiverFactory.Create("SpecialReceiver");</code></p>