How to constrain nested generic types of a generic method
<p>I'm trying to create a method which returns data from the database based on the given generic type.</p>

<p><strong>The interface:</strong> (this definition compiles)</p>

<pre><code>public interface IOrderPosition&lt;TOrder, TArticle, TOrderPosition&gt;
  where TOrder : IOrder
  where TArtile : IArticle
  where TOrderPosition : IOrderPosition&lt;TOrder, TArticle, TOrderPosition&gt;
{
  long? id { get; set; }
  TOrder order { get; set; }
  TArtile article { get; set; }
  List&lt;TOrderPosition&gt; subPositions { get; set; }
}
</code></pre>

<p><strong>A possible concrete implementation:</strong> (this definition compiles)</p>

<pre><code>public class OrderPosition : IOrderPosition&lt;Order, Article, OrderPosition&gt;
{
  public long? id { get; set; }
  public Order order { get; set; }
  public Article article { get; set; }
  public List&lt;OrderPosition&gt; subPositions { get; set; }
}
</code></pre>

<p><strong>Trying to write a generic method based on the interface:</strong> (this definition DOES NOT compile)</p>

<pre><code>public List&lt;TOrderPosition&gt; GetOrderPositionOfOrder&lt;TOrderPosition&gt;(long? id) 
  where TOrder : IOrder
  where TArticle : IArticle
  where TOrderPosition : IOrderPosition&lt;TOrder, TArticle, TOrderPosition&gt;
{
  ..
}
</code></pre>

<p><strong>Errors:</strong></p>

<pre><code>'DataSourceOrder.GetOrderPositionOfOrder&lt;TOrderPosition&gt;()' does not define type parameter 'TOrder'
'DataSourceOrder.GetOrderPositionOfOrder&lt;TOrderPosition&gt;()' does not define type parameter 'TArticle'
The type or namespace name 'TOrder' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'TArticle' could not be found (are you missing a using directive or an assembly reference?)
</code></pre>

<p><strong>To be used like this:</strong></p>

<pre><code>List&lt;OrderPosition&gt; positions = GetOrderPositionOfOrder&lt;OrderPosition&gt;(5);
List&lt;TransferOrderPosition&gt; transferPositions = GetOrderPositionOfOrder&lt;TransferOrderPosition&gt;(5);
</code></pre>

<hr>

<p><strong>Question:</strong></p>

<p>Why does this compile for the interface, but not for the method?</p>

<p>I expected both to work or both to fail. I assumed that the compile could infer the types of TOrder and TArticle from the type given for TOrderPosition which defines concrete types for both the article and order.</p>

<p>I would like to know why this happens and if and how I can solve the problem without having to specify all types explicitly.</p>