public static IEnumerable <ParentedRecursiveType <TRecursiveParent, TRecursiveType> > GetSelfAndDescendentsWithParents <TRecursiveParent, TRecursiveType>(this TRecursiveParent root, TRecursiveParent parent = default(TRecursiveParent)) where TRecursiveParent : class, IRecursiveParent <TRecursiveType> where TRecursiveType : class, IRecursiveType { Requires.NotNull(root, "root"); IRecursiveType rootAsRecursiveType = root; yield return(new ParentedRecursiveType <TRecursiveParent, TRecursiveType>((TRecursiveType)rootAsRecursiveType, parent)); var rootAsParent = root as TRecursiveParent; if (rootAsParent != null && rootAsParent.Children != null) { foreach (TRecursiveType child in rootAsParent.Children) { var childAsParent = child as TRecursiveParent; if (childAsParent != null) { foreach (var descendent in childAsParent.GetSelfAndDescendentsWithParents <TRecursiveParent, TRecursiveType>(rootAsParent)) { yield return(descendent); } } else { yield return(new ParentedRecursiveType <TRecursiveParent, TRecursiveType>(child, rootAsParent)); } } } }
/// <summary>Checks whether a given object is among this object's descendents.</summary> public static bool HasDescendent(this IRecursiveParent parent, IRecursiveType possibleDescendent) { Requires.NotNull(parent, "parent"); Requires.NotNull(possibleDescendent, "possibleDescendent"); return(HasDescendent(parent, possibleDescendent.Identity)); }
/// <summary> /// Returns a sequence starting with the given <paramref name="root"/> /// followed by its descendents in a depth-first search. /// </summary> /// <param name="root">The node at which to start enumeration.</param> /// <returns>A sequence of nodes beginning with <paramref name="root"/> and including all descendents.</returns> public static IEnumerable <IRecursiveType> GetSelfAndDescendents(this IRecursiveType root) { var rootAsParent = root as IRecursiveParent <IRecursiveType>; if (rootAsParent != null) { return(GetSelfAndDescendents(rootAsParent)); } else { return(new[] { root }); } }
int IRecursiveParentWithOrderedChildren.IndexOf(IRecursiveType value) { return(this.Children.IndexOf((TreeNode)value)); }