Exemplo n.º 1
0
        /// <summary>
        /// Gets all ancestors (parent, grandparent, etc.) and self of the specified <paramref name="node"/> in the hierarchical structure.
        /// </summary>
        /// <typeparam name="T">The type of the instance represented by the specified <paramref name="node"/> in the hierarchical structure.</typeparam>
        /// <param name="node">The node that the hierarchical structure represents.</param>
        /// <returns>An <see cref="IEnumerable{T}"/> sequence equal to ancestors and self of the specified <paramref name="node"/>.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="node"/> is null.
        /// </exception>
        public static IEnumerable <IHierarchy <T> > AncestorsAndSelf <T>(this IHierarchy <T> node)
        {
            Validator.ThrowIfNull(node, nameof(node));
            IList <IHierarchy <T> > result = new List <IHierarchy <T> >(HierarchyUtility.WhileSourceTraversalIsNotNull(node, HierarchyUtility.AncestorsAndSelf));

            return(result.Count > 0 ? result.Reverse() : node.Yield());
        }