Something like that:
internal static IEnumerable<T> AllDescendantNodes<T>( this TreeNode input ) where T class; { T current = null; foreach ( TreeNode node in input.Nodes ) if( (current = node as T) != null ) { yield return current; foreach ( var subnode in node.AllDescendantNodes<T>() ) yield return subnode; } }
Then you call this against the root of the node as an extension method:
foreach( MyCustomNodeClass item in rootNode.AllDescendantNodes<MyCustomNodeClass>() ) { ... }
source share