Return/consume dynamic anonymous type across assembly boundaries

Solution 1:

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.

Solution 2:

The cause of the problem is that anonymous types are internal to assemblies. That's why the Dynamic Language Runtime don't allow you to access properties from another assembly.

One solution is explained in this post. You can put an custom attribute in the assembly that defines the anonymous type allowing the other assembly to access its internals.

Another solution is returning an object of a public class (with public properties). That will, of course, kill the advantages of the anonymous type.

A third solution would be using an ExpandoObject as suggested by Reed Copsey.

If you still want to use the anonymous type, you could write a dynamic class that "decorates" any anonymous type and exposes its members. Such a class would have to implement the IDynamicMetaObjectProvider interface and access the decorated object via reflection. Possibly, this stuff was already implemented by someone out there.