Return/consume dynamic anonymous type across assembly boundaries

Posted by friism on Stack Overflow See other posts from Stack Overflow or by friism
Published on 2010-06-07T21:11:10Z Indexed on 2010/06/07 21:12 UTC
Read the original article Hit count: 307

Filed under:
|
|
|

The code below works great. If the Get and Use methods are in different assemblies, the code fails with a RuntimeBinderException. This is because the .Net runtime system only guarantees commonality of anonymous types (<string, int> in this case) within assemblies.

Is there any way to fool the runtime system to overcome this? I can expect the object in the debugger on the Use side, and the debugger can see the relevant properties.

class Program
{
    static void Main(string[] args)
    {
        UsePerson();
        Console.ReadLine();
    }

    public static void UsePerson()
    {
        var person = GetPerson();

        Console.WriteLine(person.Name);
    }

    public static dynamic GetPerson()
    {
        return new { Name = "Foo", Age = 30 };
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET