It's a shame that adding dynamic properties (whose name is known only at run-time) to ExpandoObject
is not as easy as it should have been. All the casting to dictionary is plain ugly. Never mind you could always write a custom DynamicObject
that implements Add
which helps you with neat object initializer like syntax.
A rough example:
public sealed class Expando : DynamicObject, IDictionary<string, object>
{
readonly Dictionary<string, object> _properties = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _properties.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (binder.Name == "Add")
{
var del = value as Delegate;
if (del != null && del.Method.ReturnType == typeof(void))
{
var parameters = del.Method.GetParameters();
if (parameters.Count() == 2 && parameters.First().ParameterType == typeof(string))
throw new RuntimeBinderException("Method signature cannot be 'void Add(string, ?)'");
}
}
_properties[binder.Name] = value;
return true;
}
object IDictionary<string, object>.this[string key]
{
get
{
return _properties[key];
}
set
{
_properties[key] = value;
}
}
int ICollection<KeyValuePair<string, object>>.Count
{
get { return _properties.Count; }
}
bool ICollection<KeyValuePair<string, object>>.IsReadOnly
{
get { return false; }
}
ICollection<string> IDictionary<string, object>.Keys
{
get { return _properties.Keys; }
}
ICollection<object> IDictionary<string, object>.Values
{
get { return _properties.Values; }
}
public void Add(string key, object value)
{
_properties.Add(key, value);
}
bool IDictionary<string, object>.ContainsKey(string key)
{
return _properties.ContainsKey(key);
}
bool IDictionary<string, object>.Remove(string key)
{
return _properties.Remove(key);
}
bool IDictionary<string, object>.TryGetValue(string key, out object value)
{
return _properties.TryGetValue(key, out value);
}
void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
{
((ICollection<KeyValuePair<string, object>>)_properties).Add(item);
}
void ICollection<KeyValuePair<string, object>>.Clear()
{
_properties.Clear();
}
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
{
return ((ICollection<KeyValuePair<string, object>>)_properties).Contains(item);
}
void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
((ICollection<KeyValuePair<string, object>>)_properties).CopyTo(array, arrayIndex);
}
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
{
return ((ICollection<KeyValuePair<string, object>>)_properties).Remove(item);
}
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
{
return _properties.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_properties).GetEnumerator();
}
}
And you could call like you wanted:
dynamic obj = new Expando()
{
{ "foo", "hello" },
{ "bar", 42 },
{ "baz", new object() }
};
int value = obj.bar;
The caveat with this approach is that you cannot add Add
"method" with the same signature as Dictionary.Add to your expando object since it is already a valid member of the Expando
class (which was required for the collection initializer syntax). The code throws an exception if you do
obj.Add = 1; // runs
obj.Add = new Action<string, object>(....); // throws, same signature
obj.Add = new Action<string, int>(....); // throws, same signature for expando class
obj.Add = new Action<string, object, object>(....); // runs, different signature
obj.Add = new Func<string, object, int>(....); // runs, different signature
If property names needn't be truly dynamic then another alternative is to have a ToDynamic
extension method so that you can initialize in-line.
public static dynamic ToDynamic(this object item)
{
var expando = new ExpandoObject() as IDictionary<string, object>;
foreach (var propertyInfo in item.GetType().GetProperties())
expando[propertyInfo.Name] = propertyInfo.GetValue(item, null);
return expando;
}
So you can call:
var obj = new { foo = "hello", bar = 42, baz = new object() }.ToDynamic();
int value = obj.bar;
There are a hundred ways you can design an API for this, another one such (mentioned in orad's answer) is:
dynamic obj = new Expando(new { foo = "hello", bar = 42, baz = new object() });
Will be trivial to implement.
Side note: there is always anonymous types if you know the property names statically and you dont want to add further after initialization.