I have an Entity
abstract generic class which main characteristic is that it can be grouped with other instances of different implementations of this same class, around a common "group id".
For example, a MoveBehaviour
and FlyBehaviour
can implement Entity
so two given instances of these classes could be assigned to a given Person
instance and FlyBehaviour
could look up its corresponding MoveBehaviour
easily.
One can argument I'm killing OOP so I should go home and never ever touch a computer again, but let's have this discussion some other day.
This is my attempt:
public abstract class Entity<T> where T : Entity<T>
{
private int groupId;
protected static Dictionary<int, T> entityMap = new Dictionary<int, T>();
public Entity(int groupId)
{
this.groupId = groupId;
entityMap.Add(groupId, (T) this);
}
public T2 GetCoEntity<T2>() where T2 : Entity<T2>
{
return T2.entityMap[groupId];
}
}
The compiler complains it doesn't know what entityMap
is and I don't understand why.