/// <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()); }
/// <summary> /// Flattens any inner exceptions descendant-or-self from the specified <paramref name="exception"/> into an <see cref="IEnumerable{T}"/> sequence of exceptions. /// </summary> /// <param name="exception">The exception to flatten.</param> /// <param name="exceptionType">The type of the specified <paramref name="exception"/>.</param> /// <returns>An empty <see cref="IEnumerable{T}"/> sequence if no inner exceptions was referenced; otherwise any inner exceptions descendant-or-self from the specified <paramref name="exception"/>.</returns> /// <exception cref="System.ArgumentNullException"> /// <paramref name="exception"/> -or <paramref name="exceptionType"/> is null. /// </exception> /// <remarks> /// If any inner exceptions are referenced this method will iterative flatten all of them descendant-or-self from the specified <paramref name="exception"/>.<br/> /// Should the <paramref name="exception"/> be of the new AggregateException type introduced with .NET 4.0, the return sequence of this method will be equal to the result of the InnerExceptions property. /// </remarks> public static IEnumerable <Exception> Flatten(Exception exception, Type exceptionType) { Validator.ThrowIfNull(exception, nameof(exception)); Validator.ThrowIfNull(exceptionType, nameof(exceptionType)); PropertyInfo innerExceptionsProperty = ReflectionUtility.GetProperty(exceptionType, "InnerExceptions"); if (innerExceptionsProperty != null) { return(innerExceptionsProperty.GetValue(exception, null) as IEnumerable <Exception>); } return(HierarchyUtility.WhileSourceTraversalHasElements(exception, FlattenCallback).Skip(1)); }
/// <summary> /// Gets all descendants (children, grandchildren, etc.) anf self of the current <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 the descendants and self of the specified <paramref name="node"/>.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="node"/> is null. /// </exception> public static IEnumerable <IHierarchy <T> > DescendantsAndSelf <T>(this IHierarchy <T> node) { Validator.ThrowIfNull(node, nameof(node)); return(HierarchyUtility.WhileSourceTraversalHasElements(node, HierarchyUtility.DescendantsAndSelf).Reverse()); }