title: How to get the subtype of a generic class in C#
body: <p>I have a generic method</p>
  
  <pre><code>public async Task Save&lt;T&gt;(T aggregate) where T : AggregateRoot
  </code></pre>
  
  <p>From this method I call another generic method</p>
  
  <pre><code>var indexRegistrations = IndexRegistrar.GetAll&lt;T&gt;();
  </code></pre>
  
  <p>Now in this second generic method, I want to get the real type of <code>T</code>, which is a subtype of <code>AggregateRoot</code>:</p>
  
  <pre><code>public static List&lt;IndexRegistration&gt; GetAll&lt;T&gt;() where T : AggregateRoot
  {
      return _register.FindAll(r =&gt; r.aggregateType == typeof(T));
  }
  </code></pre>
  
  <p>However, the <code>typeof(T)</code> always returns <code>AggregateRoot</code>.</p>
  
  <p>How can I get the real type (=subtype of <code>AggregateRoot</code>) of <code>T</code>?</p>