using System; using System.Collections.Generic; namespace ln.http.resources { public abstract class ObjectBase { string name; public ObjectBase Container { get; private set; } Dictionary children = new Dictionary(); public ObjectBase(string name) { this.name = name; } public void Add(ObjectBase child) { if (children.ContainsKey(child.Name)) throw new ArgumentException(String.Format("Container already has child with name {0}",child.Name)); children.Add(child.Name, child); } public void Remove(ObjectBase child) { if (child.Container != this) throw new ArgumentException("Can't remove child of other container"); children.Remove(child.Name); } public bool Contains(string name) { return children.ContainsKey(name); } public bool Contains(ObjectBase child) { return children.ContainsValue(child); } public IEnumerable Children { get => this.children.Values; } public String Name { get { return this.name; } set { if (Container != null) { if (Container.Contains(value)) throw new ArgumentException("Owning container already has a child with that name"); Container.children.Remove(this.name); this.name = value; Container.children.Add(this.name, this); } } } public ObjectBase this[string name] { get { return children[name]; } } } }