// ENTITY BASE CLASS
public class Entity {
private readonly Dictionary<string, object> adjunctDataSpace = new Dictionary<string, object>();
internal T GetAdjunctField<T>(string name) {
return (T) adjunctDataSpace[name];
}
internal void SetAdjunctField<T>(string name, T value) {
adjunctDataSpace[name] = value;
}
internal T GetAdjunctField<T>(string name, Func<T> defaultValueGenerator) {
if (!adjunctDataSpace.ContainsKey(name)) adjunctDataSpace[name] = defaultValueGenerator();
return GetAdjunctField<T>(name);
}
}
// COMPONENTS (INTERFACES)
public interface ITangibleEntity { }
// COMPONENTS (EXTENSION METHOD IMPLEMENTATIONS)
public static class ComponentExtensions {
// ITangibleEntity
public static Vector3 GetPosition<T>(this T @this) where T : Entity, ITangibleEntity {
return @this.GetAdjunctField("ITangibleEntity.Position", () => Vector3.Zero);
}
public static void SetPosition<T>(this T @this, Vector3 newPosition) where T : Entity {
@this.SetAdjunctField("ITangibleEntity.Position", newPosition);
}
public static Vector3 GetVelocity<T>(this T @this) where T : Entity {
return @this.GetAdjunctField("ITangibleEntity.Velocity", () => Vector3.Zero);
}
public static void SetVelocity<T>(this T @this, Vector3 newVelocity) where T : Entity {
@this.SetAdjunctField("ITangibleEntity.Velocity", newVelocity);
}
}
Code snippet taken from "Simulating Multiple Inheritance In C#".