public static IHierarchyObject GetCommonAncestor(this IHierarchyObject source, IHierarchyObject other) { if (object.ReferenceEquals(other, source)) { return source; } else if (other.IsAncestorOf(source)) { return other; } else if (other.IsDescendentOf(source)) { return source; } else { // Scan the hierarchy foreach (IHierarchyObject ancestor in GetAncestors(other)) { // Check the ancestry - skip the starting instance; we already // performed that check if (ancestor.ParentObject != null && ancestor.ParentObject.IsAncestorOf(source)) { return ancestor.ParentObject; } } return null; } }