title: C#: How to force a generic type to be an interface?
body: <p>The code below is valid:</p>
  
  <pre><code>public class Converter&lt;T,M&gt; : CustomCreationConverter&lt;M&gt;  
      where T : new()
      where M : class
  </code></pre>
  
  <p>However, this is what I would like to do:</p>
  
  <pre><code>public class Converter&lt;T,M&gt; : CustomCreationConverter&lt;M&gt;  
      where T : new()
      where M : interface
  </code></pre>
  
  <p><strong>Is something equivalent to this possible?</strong></p>
  
  <p>A constraint that specified that T could be cast as M would also suffice in my specific circumstance.</p>
  
  <p>To clarify, I am <em>not</em> looking for the ability to make <code>T</code> implement some <em>specific</em> interface.</p>
  
  <p>Someone may try to mark this question as a duplicate of "<a href="https://stackoverflow.com/questions/3600696/force-generic-interface-implementation-in-c-sharp">Force generic interface implementation in C#</a>". Here are the reasons why I believe it is worth not marking my question a duplicate:</p>
  
  <ol>
  <li>That question is over 8 years old</li>
  <li>The question was resolved when the person posting an answer discovered that the person posting the question actually wanted to know how to implement a <em>specific</em> interface</li>
  <li>If this is not possible through constraints, there may be a workaround</li>
  </ol>
  
  <hr>
  
  <p>The reason I'm trying to do this is because I'm trying to create multiple custom JSON converters using <a href="https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverter.htm" rel="nofollow noreferrer">Json.NET's JsonConverter Class</a>.</p>
  
  <pre><code>public class Converter&lt;T,M&gt; : CustomCreationConverter&lt;M&gt;  
      where T : new()
  {
      public override bool CanRead =&gt; base.CanRead;
  
      public override bool CanWrite =&gt; base.CanWrite;
  
      public override bool CanConvert(System.Type objectType)
      {
          return base.CanConvert(objectType);
      }
  
      public override M Create(System.Type objectType)
      {
          return new T(); //Cannot implicity convert type 'T' to 'M'
      }
  
      public override bool Equals(object obj)
      {
          return base.Equals(obj);
      }
  
      public override int GetHashCode()
      {
          return base.GetHashCode();
      }
  
      public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer)
      {
          return base.ReadJson(reader, objectType, existingValue, serializer);
      }
  
      public override string ToString()
      {
          return base.ToString();
      }
  
      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
          base.WriteJson(writer, value, serializer);
      }
  }
  </code></pre>
  
  <p>I could do the above or <code>public class MyClassConverter : CustomCreationConverter&lt;IMyClass&gt;{}</code> for every class I need a converter for. Just trying to keep things DRY!</p>