Return/consume dynamic anonymous type across assembly boundaries

Use an ExpandoObject instead of an anonymous type. This should allow you to cross assembly boundaries safely:

public static dynamic GetPerson()
    dynamic person = new ExpandoObject();
    person.Name = "Foo";
    person.Age = 30;

    return person;

In general, anonymous types should really only be used within the same method in which they are generated. Returning an anonymous type from a method is, in general, going to cause more problems than it solves.

Leave a Comment