title: .Net Extension method for derived class (TreeNode)
body: <p>I have a treeview, and want to simply return the deepest node that satisfies a given condition.</p>
  
  <p>The answer to this question is the most promising so far:
    <a href="https://stackoverflow.com/questions/7062882/searching-a-tree-using-linq">Searching a tree using LINQ</a></p>
  
  <p>So I could do this:</p>
  
  <pre><code>foreach(CustomTreeNode aNode in treMyTreeView.Nodes){
  
      List&lt;TreeNode&gt; EligibleNodes = 
          aNode.Descendants().Where(node =&gt; node.Tag == SomeCondition)
  
  }
  </code></pre>
  
  <p><em>(I realise I might have a bit more work to do to cast from TreeNode to CustomTreeNode)</em></p>
  
  <p>But before I even get there, I'm getting stuck trying to add the extension method to the TreeNode class.</p>
  
  <pre><code>public class CustomTreeNode : TreeNode  {
  
      static IEnumerable&lt;TreeNode&gt; Descendants(this TreeNode root) {
          var nodes = new Stack&lt;TreeNode&gt;(new[] { root });
          while (nodes.Count &gt; 0) {
              TreeNode node = nodes.Pop();
              yield return node;
              foreach (TreeNode n in node.Nodes) nodes.Push(n);
          }
      }
  
  }
  </code></pre>
  
  <p>You'll tell me it needs to be static, and therefore I can't derive from TreeNode. And I don't understand why.</p>
  
  <p>How can I achieve the above (or something similar)?</p>