How to set a property of a C# 4 dynamic object when you have the name in another variable
- by Kieran Benton
I'm looking for a way to modify properties on a dynamic C# 4.0 object with the name of the property known only at runtime.
Is there a way to do something like (ExpandoObject is just used as an example, this could be any class that implements IDynamicMetaObjectProvider):
string key = "TestKey";
dynamic e = new ExpandoObject();
e[key] = "value";
Which would be equivalent to:
dynamic e = new ExpandoObject();
e.TestKey = "value";
Or is the only way forward reflection?