反映在ExpandoObject上 [英] Reflect on an ExpandoObject

查看:171
本文介绍了反映在ExpandoObject上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个nifty函数,它将接受 system.object ,反映其属性并将对象序列化为JSON字符串。它看起来像这样:

I have written a nifty function that will accept a system.object, reflect on its properties and serialize the object into a JSON string. It looks like this:

public class JSONSerializer
{

    public string Serialize(object obj)



现在,我想能够做到这一点序列化动态/ ExpandoObject,我的序列化程序使用反射,它不能做到。解决方法是什么?

Now, I want to be able to do this to serialize a dynamic/ExpandoObject, but because my serializer uses reflection, it isn't able to do it. What's the workaround?

public class Test
{
    public dynamic MakeDynamicCat()
    {
        dynamic newCat = new ExpandoObject();
        newCat.Name = "Polly";
        newCat.Pedigree = new ExpandoObject();
        newCat.Pedigree.Breed = "Whatever";

        return newCat;
    }

    public void SerializeCat()
    {
        new JSONSerializer().Serialize(MakeDynamicCat());
    }
}


推荐答案

I认为,这个问题非常相似: http:// stackoverflow.com/questions/2634858/how-do-i-reflect-over-the-members-of-dynamic-object

I think, this question is very similar: http://stackoverflow.com/questions/2634858/how-do-i-reflect-over-the-members-of-dynamic-object

至少答案也应该帮助你。

At least the answers should help you too.

这篇关于反映在ExpandoObject上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆