no definition for & ldquo; Action & rdquo; on a dynamic?

I've got an event here that is fired with an argument that contains a dynamic object. It's an anonymous object directly created in the event call itself, like

if(MyEvent != null) MyEvent(this, new SensoricDataEventArgs { Data = new { Action = "Start", Team = "Blau" } });

where SensoricDataEventArgs's signature is something like this:

class SensoricDataEventArgs : EventArgs {
  public dynamic Data { get; set; }
}

When I try to access that data in a handler (which is in another assembly, in case that's relevant), all I get is an exception that object doesn't have a definition for Action, even though I re-cast the Data property to dynamic. Even the DebuggerVisualizer for dynamic objects shows that there is a property Action on the object.

However, I get the mentioned exception.

Here's an image of what I'm trying to do in the event handler, all together with the info from the debugger. In the image the debugger has stopped on a breakpoint. The next step throws the exception.

Any idea of what causes this exception? What am I doing wrong?


The problem is that anonymous types are declared internal, so the members you are trying to access are not visible in your other assembly and the DLR actually does the same compile-time analysis on member accessibility at run-time. It will not let you access at run-time something you could not access at compile-time. It sees the anonymous type is internal and throws a RuntimeBinderException.

One solution is to write the reflection code explicitly rather than using dynamic. A more maintainable, long-term solution would be to remove dynamic from the event args and define more precisely how your components are communicating with each other.