public List<InstancePathElement> findSingleObjectPath(object root, object object2Find) { bool found=false; ObjectExplorerImpl explorer = new ObjectExplorerImpl(); Stack<InstancePathElement> currentPath = new Stack<InstancePathElement>(); MoveAway down = delegate(Object from, string propertyName, Object to, bool isIndexed, int? index) { if (found) return false; InstancePathElement element; InstancePathElement thepeek = currentPath.Count>0 ? currentPath.Peek() : null; bool parentIsIndexed = currentPath.Count > 0 && currentPath.Peek().IsIndexed; if (parentIsIndexed) { IndexPathElement ielement = new IndexPathElement(); ielement.Index = (int)index; element = ielement; } else { NamePathElement nElement = new NamePathElement(); nElement.Name = propertyName; element = nElement; } element.IsIndexed = isIndexed; currentPath.Push(element); found = object2Find == to; return (!found); }; MoveBack up = (from, propertyName, to, isIndexed) => { if (!found) currentPath.Pop(); }; OnLeaf onLeaf = (from, propertyName, to, index) => { }; PropertyReflectionNodeExpander expander = new PropertyReflectionNodeExpander(); expander.ExcludeReadOnlyProperties = true; explorer.NodeExpander = expander; explorer.explore(root, down, up, onLeaf); List<InstancePathElement> result = null; if (found) { result = new List<InstancePathElement>(currentPath.ToArray().Reverse()); // remove the first element because it is to the root result.RemoveAt(0); } return result; }
//, TypeAliaser aliaser public LeafDefaultSet getDefaultsForAllLinkedObjects(Object root, NodeExpander nodeExpander) { LeafDefaultSet result = new LeafDefaultSet(); ObjectExplorerImpl explorer = new ObjectExplorerImpl(); MoveAway down = (from, propertyName, to, isIndexed, index) => { return(isIndexed || (to != null && !result.Type2Defaults.ContainsKey(to.GetType()))); }; MoveBack up = (from, propertyName, to, isIndexed) => { }; OnLeaf leaf = (from, propertyName, to, index) => { Defaults4Class defaults = null; if (from == null) { return; } Type fromType = from.GetType(); if (!result.Type2Defaults.ContainsKey(fromType)) { defaults = new Defaults4Class(); defaults.FullClassName = fromType.FullName; result.Type2Defaults[fromType] = defaults; } else { defaults = result.Type2Defaults[fromType]; } defaults.PropertyName2DefaultValue[propertyName] = to; }; explorer.NodeExpander = nodeExpander; explorer.explore(root, down, up, leaf); return(result); }
//, TypeAliaser aliaser public LeafDefaultSet getDefaultsForAllLinkedObjects(Object root, NodeExpander nodeExpander) { LeafDefaultSet result = new LeafDefaultSet(); ObjectExplorerImpl explorer = new ObjectExplorerImpl(); MoveAway down = ( from, propertyName, to, isIndexed, index) => { return (isIndexed || (to != null && !result.Type2Defaults.ContainsKey(to.GetType()))); }; MoveBack up = (from, propertyName, to, isIndexed) => { }; OnLeaf leaf = (from, propertyName, to, index) => { Defaults4Class defaults = null; if (from==null) {return;} Type fromType = from.GetType(); if (!result.Type2Defaults.ContainsKey(fromType)) { defaults = new Defaults4Class(); defaults.FullClassName = fromType.FullName; result.Type2Defaults[fromType] = defaults; } else { defaults = result.Type2Defaults[fromType]; } defaults.PropertyName2DefaultValue[propertyName] = to; }; explorer.NodeExpander = nodeExpander; explorer.explore(root, down, up, leaf); return result; }