title: Dynamic derive Generic Type Parameter
body: <p>I am using a recursive function for a object tree. That is, my object collection is like:</p>
  
  <pre><code>Object1
  --Object2
  ----Object3
  ------Object4
  </code></pre>
  
  <p>All objects inherits from a base object (abstract class), where has a Validate() method, and its collection inherits from ITreeCollection. I have written a recursive function to perform that:</p>
  
  <pre><code>private bool Validate&lt;T&gt;(ITreeCollection items) where T : TreeNodeBase
          {
              foreach (var itm in items as Collection&lt;T&gt;)
              {
                  if (((TreeNodeBase)itm).Items != null)
                  {
                      return Validate&lt;T&gt;(((TreeNodeBase)itm).Items);
                  }
                  else return true;
              }
              return true;
          }
  </code></pre>
  
  <p>How can I derive the the type parameter T for the inner function (i.e. <code>return Validate&lt;T&gt;(((TreeNodeBase)itm).Items)</code>)</p>