ObjectExtensions

namespace Core.Utility
{
   using System;
   using System.Collections.Generic;


   public static class ObjectExtensions
   {
      public static IEnumerable<T> FlattenHierarchy<T>(this T node, Func<T, IEnumerable<T>> getChildEnumerator)
      {
         yield return node;
         if (getChildEnumerator(node) == null)
         {
            yield break;
         }

         foreach (var child in getChildEnumerator(node))
         {
            foreach (var childOrDescendant in child.FlattenHierarchy(getChildEnumerator))
            {
               yield return childOrDescendant;
            }
         }
      }
   }
}