Пример #1
0
        static void Main(string[] args)
        {
            //-Ancestor
            //--Child
            //---Grandchild (call full name here)
            SampleHierachyObject ancestor = new SampleHierachyObject("Ancestor");
            SampleHierachyObject child = new SampleHierachyObject("Child");
            SampleHierachyObject grandchild = new SampleHierachyObject("Grandchild");
            SampleHierachyObject sibling1 = new SampleHierachyObject("Sibling1");
            SampleHierachyObject sibling2 = new SampleHierachyObject("Sibling2");

            ancestor.Parent = ancestor; // this will be ignored, could have potentially caused an infinite loop

            child.Parent = ancestor;

            grandchild.Parent = child;
            sibling1.Parent = grandchild;
            sibling2.Parent = grandchild;

            IEnumerable<SampleHierachyObject> descendants = ancestor.Descendants.ToList();

            string fullName1 = ancestor.GetFullName();
            string fullName2 = child.GetFullName();
            string fullName3 = grandchild.GetFullName();

            var siblings1 = ancestor.Siblings;
            var res = sibling2.Siblings;
        }
 private bool TryRemoveChild(SampleHierachyObject sampleHierachyObject)
 {
     return this.children.Remove(sampleHierachyObject);
 }
 private void AddChild(SampleHierachyObject child)
 {
     this.children.Add(child);
 }
        public string GetFullName(bool useCachedValue = false)
        {
            if (!useCachedValue || this.fullName == string.Empty)
            {
                StringBuilder builder = new StringBuilder();
                builder.Insert(0, this.Name);
                SampleHierachyObject parent = this.Parent;

                while (parent != null && parent.Id != this.Id)
                {
                    builder.Insert(0, separator);
                    builder.Insert(0, parent.Name);
                    //avoid infinite loop here by ensuring parent is not same as child
                    //or enforce it on setting parent - chose the latter
                    parent = parent.Parent;
                }
                this.fullName = builder.ToString();
            }
            return this.fullName;
        }