Reflect on an ExpandoObject
Asked Answered
S

1

7

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());
    }
}
Spitzer answered 9/6, 2010 at 10:27 Comment(1)
possible duplicate of How do I reflect over the members of dynamic object?Inactivate
T
2

I think, this question is very similar: How do I reflect over the members of dynamic object?

At least the answers should help you too.

Terrell answered 10/6, 2010 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.