title: How to get the type of T from a member of a generic class or method
body: <p>Let's say I have a generic member in a class or method, like so:</p>
  <pre><code>public class Foo&lt;T&gt;
  {
      public List&lt;T&gt; Bar { get; set; }
      
      public void Baz()
      {
          // get type of T
      }   
  }
  </code></pre>
  <p>When I instantiate the class, the <code>T</code> becomes <code>MyTypeObject1</code>, so the class has a generic list property: <code>List&lt;MyTypeObject1&gt;</code>. The same applies to a generic method in a non-generic class:</p>
  <pre><code>public class Foo
  {
      public void Bar&lt;T&gt;()
      {
          var baz = new List&lt;T&gt;();
          
          // get type of T
      }
  }
  </code></pre>
  <p>I would like to know what type of objects the list of my class contains. So what type of <code>T</code> does the list property called <code>Bar</code> or the local variable <code>baz</code> contain?</p>
  <p>I cannot do <code>Bar[0].GetType()</code>, because the list might contain zero elements. How can I do it?</p>