title: How to instantiate a class as the interface that it derives from with constrained generic type parameter
body: <p>There's the following interface which defines a packet.</p>
  
  <pre><code>public interface IPacket
  {
      int Size { get; }
  }
  </code></pre>
  
  <p>There are two implementations, each with its own additional property.</p>
  
  <pre><code>public class FooPacket : IPacket
  {
      public int Size =&gt; 10;
      public string FooProperty { get; set; }
  }
  
  public class BarPacket : IPacket
  {
      public int Size =&gt; 20;
      public string BarProperty { get; set; }
  }
  </code></pre>
  
  <p>The above is library code I have no control over. I want to create a handler for packets</p>
  
  <pre><code>public interface IPacketHandler&lt;T&gt; where T : IPacket
  {
      void HandlePacket(T packet) ;
  }
  </code></pre>
  
  <p>and create two implementations for the concrete packets.</p>
  
  <pre><code>public class FooPacketHandler : IPacketHandler&lt;FooPacket&gt;
  {
      public void HandlePacket(FooPacket packet) { /* some logic that accesses FooProperty */ }
  }
  
  public class BarPacketHandler : IPacketHandler&lt;BarPacket&gt;
  {
      public void HandlePacket(BarPacket packet) { /* some logic that accesses BarProperty */ }
  }
  </code></pre>
  
  <p>I'd like to inject a list of packet handlers into a class that manages packet handling so that it can be extended in the future with additional packet handlers.</p>
  
  <pre><code>public class PacketHandlerManager
  {
      public PacketHandlerManager(IEnumerable&lt;IPacketHandler&lt;IPacket&gt;&gt; packetHandlers)
      {
  
      }
  }
  </code></pre>
  
  <p>The trouble I'm having is when creating the injected parameter. I cannot do</p>
  
  <pre><code>var packetHandlers = new List&lt;IPacketHandler&lt;IPacket&gt;&gt;
  {
      new FooPacketHandler(),
      new BarPacketHandler()
  };
  </code></pre>
  
  <p>because I cannot create an instance like so:</p>
  
  <pre><code>IPacketHandler&lt;IPacket&gt; packetHandler = new FooPacketHandler();
  </code></pre>
  
  <p>I get the error <code>Cannot implicitly convert type 'FooPacketHandler' to 'IPacketHandler&lt;IPacket&gt;. An explicit conversion exists (are you missing a cast?)</code></p>
  
  <p>I had a look at a similar question: <a href="https://stackoverflow.com/questions/17956407/casting-generic-type-with-interface-constraint">Casting generic type with interface constraint</a>. In that question, OP didn't show the members of the interface, only the definition of it from a generics point of view. From what I can see, if my interface didn't use the generic type parameter as an input, I could make it covariant using the <code>out</code> keyword, but that doesn't apply here.</p>
  
  <p>How do I achieve making manager adhere to the open-closed principle? Is my only recourse changing the interface definition to</p>
  
  <pre><code>public interface IPacketHandler
  {
      void HandlePacket(IPacket packet);
  }
  </code></pre>
  
  <p>and then casting to a particular packet in the implementation?</p>