Last active
December 11, 2015 19:28
-
-
Save hagbarddenstore/4648314 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Component | |
{ | |
private readonly IDictionary<string, object> _properties = new Dictionary<string, object>(); | |
public Component() | |
{ | |
} | |
public Component(IEnumerable<KeyValuePair<string, object>> properties) | |
{ | |
_properties = new Dictionary<string, object>(properties); | |
} | |
public string Type | |
{ | |
get { return this["_type"]; } | |
protected set { this["_type"] = value; } | |
} | |
protected object this[string key] | |
{ | |
get | |
{ | |
return _properties[key]; | |
} | |
set | |
{ | |
AssertIsSimpleType(value); | |
_properties[key] = value; | |
} | |
} | |
public Component Clone() | |
{ | |
var component = new Component(_properties); | |
return component; | |
} | |
protected T GetItem<T>(string key) | |
{ | |
var item = (T)this[key]; | |
return item; | |
} | |
private void AssertIsSimpleType(object item) | |
{ | |
// Expand this later... | |
var isSimpleType = item is string || | |
item is int || | |
item is long || | |
item is double || | |
item is Guid; | |
if (!isSimpleType) | |
{ | |
throw new InvalidTypeException(); | |
} | |
} | |
} | |
class Position : Component | |
{ | |
public Position() | |
{ | |
Type = "Position"; | |
} | |
public double X | |
{ | |
get { return GetItem<double>("X"); } | |
set { this["X"] = value; } | |
} | |
public double Y | |
{ | |
get { return GetItem<double>("Y"); } | |
set { this["Y"] = value; } | |
} | |
public double Z | |
{ | |
get { return GetItem<double>("Z"); } | |
set { this["Z"] = value; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment