Is there a way to dynamically access the property of an expando using a "IDictionary" style lookup?
var messageLocation = "Message";
dynamic expando = new ExpandoObject();
expando.Message = "I am awesome!";
Console.WriteLine(expando[messageLocation]);
You have to cast the ExpandoObject
to IDictionary<string, object>
:
var messageLocation = "Message";
dynamic expando = new ExpandoObject();
expando.Message = "I am awesome!";
var expandoDict = (IDictionary<string, object>)expando;
Console.WriteLine(expandoDict[messageLocation]);
(Also your expando variable must be typed as dynamic
so property access is determined at runtime - otherwise your sample won't compile)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With