ln.http.resources/ObjectBase.cs

72 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
namespace ln.http.resources
{
public abstract class ObjectBase
{
string name;
public ObjectBase Container { get; private set; }
Dictionary<string, ObjectBase> children = new Dictionary<string, ObjectBase>();
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<ObjectBase> 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];
}
}
}
}