System.Delegate missing Method Property in .Net for Metro Style Apps

This post is part of a series of ‘Lessons Learnt’ from  a recent conversion of Microsoft.Practices.Prism to Net Metro Prism. See here for related posts

I got stumped on an issue with delegates whilst converting Prism to Metro  … In metro, A System.Delegate type doesn’t have a Method property. How can that be!?

Background

One of the internal classes in the Prism library (DelegateReference.cs) acts a weak Delegate reference… It takes a Delegate type parameter in its constructor and records all of its information . It then allows the delegate to be freed from memory until the point it is meant to be fired; at which point it re-creates the delegate using the information it recorded.

I believe its used as part of the EventAggregator functionality to avoid having lots of active event handlers .

Problem

The existing  constructor attempts to record the MethodInfo from the Delegate using the following line of code.

string methodName = @delegate.Method;

In .Net for Metro this Method property is not exposed

……!?

My initial thought was that the delegate must know which method to invoke; so even though it is not exposed on the abstract Delegate class; it must be recorded a private variable. Perhaps the derived class will expose the method.

So I started the debugger; and used the immediate window to inspect the delegate as it was passed to the ctor.

clip_image001

Notice that you can see the Method property – [void DoEvent(System.String).

If I look at the base System.Delegate class then I can also see the same method:

clip_image002

The property is there and i can see the event. Yet intellisense and the compiler won’t allow me to access it.

…..!? Does anyone know what is causing this?

My initial thought is that there is a mismatch between the assemblies that Visual Studio/Compiler is using; and those that are in use from the GAC at runtime.

Workaround

Instead of using a strongly typed Delegate; I referred to it using the dynamic keyword instead. This avoids the compiler checking; and allows me to write the code that queries the method property. As the Method property is in memory when running the app the code works as it should.