I need to get a dictionary of properties and their values from an object declared with the dynamic keyword in .NET 4? It seems using reflection for this will not work.
Example:
dynamic s = new ExpandoObject();
s.Path = "/Home";
s.Name = "Home";
// How do I enumerate the Path and Name properties and get their values?
IDictionary<string, object> propertyValues = ???
In the case of ExpandoObject, the ExpandoObject class actually implements IDictionary<string, object>
for its properties, so the solution is as trivial as casting:
IDictionary<string, object> propertyValues = (IDictionary<string, object>)s;
Note that this will not work for general dynamic objects. In these cases you will need to drop down to the DLR via IDynamicMetaObjectProvider.