title: C# Failing Covariant Cast
body: <p>I'm trying to create something like a generic table factory with a range of implementations. Example below is self explanatory, and the cast in the end isn't working even though both type attributes marked as 'out'. I suppose, the issue is that Table isn't an interface and this breaks the cast. But it shuldn't really be interface and especially not covariant. Any ideas how to cast it properly? Definitely don't want to use reflection to use methods.</p>
  
  <pre><code>interface ITableRow { }
  
  class Table&lt;T&gt; where T : ITableRow { }
  
  interface ITableFactory&lt;out TRow, out TTable&gt;
      where TRow : ITableRow
      where TTable : Table&lt;TRow&gt;
  {
      TTable CreateTable();
  }
  
  // example implementation:
  
  class SuperRow : ITableRow { }
  
  class SuperTableFactory : ITableFactory&lt;SuperRow, Table&lt;SuperRow&gt;&gt;
  {
      public Table&lt;SuperRow&gt; CreateTable() { throw new NotImplementedException(); }
  }
  
  // run:
  
  class VarianceTest
  {
      public static void Test()
      {
          var factory = Activator.CreateInstance(Type.GetType("Snippets.Var.SuperTableFactory"));
  
          var casted = (ITableFactory&lt;ITableRow, Table&lt;ITableRow&gt;&gt;) factory; // cast fails
      }
  }
  </code></pre>