Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Last active December 11, 2015 19:28
Show Gist options
  • Save hagbarddenstore/4648314 to your computer and use it in GitHub Desktop.
Save hagbarddenstore/4648314 to your computer and use it in GitHub Desktop.
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