/// <summary> /// Will gather all children from this TreeElement which pass the provided filter /// </summary> /// <typeparam name="T">a generic TreeElement child class</typeparam> /// <param name="element">the root element for the parsing</param> /// <param name="filter">a filtering delegate</param> /// <returns>an array of TreeElement as T type</returns> public static T[] GetChildren <T>(this T rootElement, ChildFilter <T> filter) where T : TreeElement { List <T> children = new List <T>(); foreach (var child in rootElement.Children) { if (filter.Invoke(child as T)) { children.Add(child as T); } children.AddRange(((T)child).GetChildren <T>(filter)); } return(children.ToArray()); }
/// <summary> /// This helper try to find an element with the provided id in this TreeElement's hierarchy, including itself /// </summary> /// <typeparam name="T">a TreeElement generic class</typeparam> /// <param name="rootElement">the root element for the search</param> /// <param name="filter">the filtering predicate</param> /// <returns>casted TreeElement as T, or null if not found</returns> public static T Find <T>(this T rootElement, ChildFilter <T> filter) where T : TreeElement { if (filter.Invoke(rootElement as T)) { return(rootElement); } foreach (var child in rootElement.Children) { var itFound = ((T)child).Find <T>(filter); if (itFound != null) { return(itFound); } } return(null); }