I want to use MVVM in a WinRT (Windows 8) app, and one of my requirements is to be able to hook events up to commands (ICommand). This means I have to dynamically add a handler to a WinRT event. There is a nice explanation of how to do that here, but my problem is that the handler type is not known at compile time (ie. it is not always RoutedEventHandler as in that example).
I started to write a generic implementation of that code, where I build the delegates using expression trees. That part works. My problem is that invoking WindowsRuntimeMarshal.AddEventHandler dynamically fails:
var rtMarshalType = typeof (WindowsRuntimeMarshal);
var eventHandlerMethod = rtMarshalType.GetRuntimeMethods().Single(x => x.IsStatic && x.Name == "AddEventHandler");
MethodInfo closedAddMethod = eventHandlerMethod.MakeGenericMethod(handlerType);
closedAddMethod.Invoke(null, new object[] {add, remove, handler});
This fails on the Invoke call and throws an InvalidOperationException with the message:
The API
‘System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeMarshal.AddEventHandler[ItemClickEventHandler](System.Func2[Windows.UI.Xaml.Controls.ItemClickEventHandler,System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken],1[System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken],
System.Action
Windows.UI.Xaml.Controls.ItemClickEventHandler)’ cannot be used on the
current platform. See http://go.microsoft.com/fwlink/?LinkId=248273
for more information.
I know I have the right types, because when I replace the above 4 lines of code with this (hard-coding the event handler type, which is not what I want), then the code works and the event gets attached as expected:
WindowsRuntimeMarshal.AddEventHandler<ItemClickEventHandler>(add, remove, handler);
For reference, the three parameters is defined as follows (to be sure, it is not a problem with my expression tree building code, I am currently using the explicit definition of these delegates even when trying to dynamic invoke AddEventHandler, still failing):
Func<ItemClickEventHandler, EventRegistrationToken> add =
a => (EventRegistrationToken) eventInfo.AddMethod.Invoke(instance, new object[] {a});
Action<EventRegistrationToken> remove = a => eventInfo.RemoveMethod.Invoke(instance, new object[] {a});
ItemClickEventHandler handler = (s, args) => command.Execute(args);
Why does the call fail when invoked via reflection and not when I call it directly ?
Is there an alternate solution for dynamically attaching a WinRT event, when the type of event handler is not known at compile time ?
Answer:
WindowsRuntimeMarshal.AddEventHandleris marked with[SecurityCritical]attributeFrom Security Considerations for Reflection: http://msdn.microsoft.com/en-us/library/stfy7tfc.aspx
So, you cannot use reflection security-critical methods like
AddEventHandlereven if they are public. You can freely call such methods without using reflection and you can freely reflect your own methods. This is why your solution works.