How do I reflect over the members of dynamic object?

c#, dynamic, reflection

Solution

If the IDynamicMetaObjectProvider can provide the dynamic member names, you can get them. See GetMemberNames implementation in the apache licensed PCL library Dynamitey (which can be found in nuget), it works for `ExpandoObject`s and `DynamicObject`s that implement `GetDynamicMemberNames` and any other `IDynamicMetaObjectProvider` who provides a meta object with an implementation of `GetDynamicMemberNames` without custom testing beyond `is IDynamicMetaObjectProvider`.

After getting the member names it's a little more work to get the value the right way, but Impromptu does this but it's harder to point to just the interesting bits and have it make sense. Here's the documentation and it is equal or faster than reflection, however, unlikely to be faster than a dictionary lookup for expando, but it works for any object, expando, dynamic or original - you name it.

Problem

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 = ??? ```

Original source