You need to make your own ExpandoObject, by inheriting DynamicObject
and overriding
public override bool TryGetMember(GetMemberBinder binder, out object result)
and public override bool TrySetMember(SetMemberBinder binder, object value)
Implement TrySetMember
to store the value in a private Dictionary<string,object>
under binder.Name
and use TryGetMember
to retrieve it from that dictionary, that will give you a basic ExpandoObject. Then to give it the feature you need, add a check in TryGetMember
, after you pull the object, to see if it is Delagate
and then use reflection to see if it doesn't take any arguments. If both are true just cast to dynamic
and add no arg invocation parenthesis and assign it to result
.
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (_dictionary.TryGetValue(binder.Name, out result)){
if(result is Delegate && /* some reflection check on args*/){
result = ((dynamic)result)();
}
}
}
I have an open source framework ImpromptuInterface (in nuget) that has a non-sealed ImpromptuDictionary
which you could start with as your ExpandoObject
instead, especially if you need any of the more nuanced features of ExpandoObject
such as gui binding support. It also has more dlr plumbing features you might find useful.