I am having a bit of difficulty in understanding how the Container/Component model interacts with each other in C#. I get how the Component contains a Site object which has information about Container and Component. But, suppose I had the following code:
using System;
using System.ComponentModel;
public class Entity : Container {
public string Foo = "Bar";
}
public class Position : Component {
public int X, Y, Z;
public Position(int X, int Y, int Z){
this.X = X;
this.Y = Y;
this.Z = Z;
}
}
public class Program {
public static void Main(string[] args) {
Entity e = new Entity();
Position p = new Position(10, 20, 30);
e.Add(p, "Position");
}
}
This works without issue, it defines a Container (Entity) and a Component (Position) that is contained inside it.
However, if I invoke p.Site.Container
, it will return Entity, but as IContainer. That is, I would have to explicitly do something like (Console.WriteLine(p.Site.Container as Entity).Foo);
if I wanted to access Foo. This seems quite cumbersome.
Am I missing something, or is there a better way to do what I want?
You're not missing anything. There is no interface contract regarding what container a component can be inside. If you want to restrict what kind of components can be added to the container you can overload the Add method and do a check of the type of component being added:
public class Entity : Container {
public string Foo = "Bar";
public virtual void Add(IComponent component) {
if (!typeof(Position).IsAssignableFrom(component.GetType())) {
throw new ArgumentException(...);
}
base.Add(component);
}
}