Access to Dynamic object properties by name

Viewed 7155

I have created this dynamic object, and I want to access the properties by name:

dynamic obj = new ExpanadoObject();
obj.Name = "Reza";

What I want is

obj["Name"] = "Reza";

or

var name = obj["Name"];

How can I do that?

3 Answers

TryGetValue is so handy provided by IDictionary

dynamic obj = new ExpandoObject();
IDictionary<string, object> dictionary = obj;
dictionary["Name"] = "Reza";
dict.TryGetValue("Name", out object name);
Related