I\'ve noticed that the new ExpandoObject
implements IDictionary
which has the requisite IEnumerable
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
{
readonly Dictionary _properties = new Dictionary();
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.this[string key]
{
get
{
return _properties[key];
}
set
{
_properties[key] = value;
}
}
int ICollection>.Count
{
get { return _properties.Count; }
}
bool ICollection>.IsReadOnly
{
get { return false; }
}
ICollection IDictionary.Keys
{
get { return _properties.Keys; }
}
ICollection
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(....); // throws, same signature
obj.Add = new Action(....); // throws, same signature for expando class
obj.Add = new Action(....); // runs, different signature
obj.Add = new Func(....); // 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;
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.