How do I reflect over the members of dynamic object?

前端 未结 4 793
感动是毒
感动是毒 2020-11-22 10:51

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.

4条回答
  •  故里飘歌
    2020-11-22 11:01

    In the case of ExpandoObject, the ExpandoObject class actually implements IDictionary for its properties, so the solution is as trivial as casting:

    IDictionary propertyValues = (IDictionary)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.

提交回复
热议问题