Reflect on an ExpandoObject

Posted by Water Cooler v2 on Stack Overflow See other posts from Stack Overflow or by Water Cooler v2
Published on 2010-06-09T10:27:53Z Indexed on 2010/06/09 10:32 UTC
Read the original article Hit count: 1062

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)

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());
    }
}

© Stack Overflow or respective owner

Related posts about reflection

Related posts about dynamic