Pregunta

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], System.Action1[System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken], 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 ?

¿Fue útil?

Solución

Why does the call fail when invoked via reflection and not when I call it directly ?

Answer: WindowsRuntimeMarshal.AddEventHandler is marked with [SecurityCritical] attribute

From Security Considerations for Reflection: http://msdn.microsoft.com/en-us/library/stfy7tfc.aspx

Subject to necessary permissions, code can use reflection to perform the following kinds of access: Access public members that are not security-critical.

So, you cannot use reflection security-critical methods like AddEventHandler even 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.

Otros consejos

I found a way of doing this, after some tinkering about:

First, I declared this method:

    private static void AddEventHandler<T>(Func<T,EventRegistrationToken> add, Action<EventRegistrationToken> remove, T handler)
    {
        WindowsRuntimeMarshal.AddEventHandler(add, remove, handler);            
    }

And then I can call that via reflection:

    var closedAddMethod = GetAddEventHandlerMethodInfo(handlerType);
    closedAddMethod.Invoke(null, new[] {add, remove, actualDelegate});

Where GetAddEventHandlerMethodInfo is:

    private static MethodInfo GetAddEventHandlerMethodInfo(Type handlerType)
    {
        var rtMarshalType = typeof (AttachedCommand);
        var eventHandlerMethod = rtMarshalType.GetRuntimeMethods().Single(x => x.IsStatic && x.Name == "AddEventHandler");
        MethodInfo closedAddMethod = eventHandlerMethod.MakeGenericMethod(handlerType);
        return closedAddMethod;
    }

This works. As mentioned in the question, this does not work with out the intermediate method on my own class (ie. with WindowsRunTimeMarshal.AddEventHandler as the direct target of the MethodInfo). I cannot explain why this is the case, but this is a viable workaround, so I'll leave it here if anyone else has the same problem.

I don't know if this happens because Microsoft tries to guard against some unintended use, or something else - if anyone actually knows why the former method described in the original question does not work; please leave an answer.

Edit: The reflected method does not work because of platform security, just like Silverlight was to ensure applications run with in the sandbox and don't effect the .net core framework or exploit the framework to over come the security measures put in place. This unfortunately effect other things like genuine uses of the functionality. Microsoft will not let you reflect into platform classes and methods that could potentially be exploited.

Unfortunately like Silverlight some part of WinRT are locked down to keep applications in the sandboxed but that is not the whole reason however some API are still not developed.

Driis you are right your best bet is to create a wrapper method that encapsulates the AddEventHandler.

You can see a working example below, it would be simple to create the write reflection call to AssignEvent. This example assumes you have a textblock called "hellow" and a button called "button" on the screen.

public MainPage()
{
    this.InitializeComponent();

    RoutedEventHandler handler = (a, b) => this.Hellow.Text = "MUAHAHAH";
    // you can quite easily reflect the method below.
    this.AssignEvent<RoutedEventHandler>(this.button, "Click", handler);
}

private void AssignEvent<T1>(object instance, string eventName, T1 handler)
{
    var runtimeEvent = instance.GetType().GetRuntimeEvent(eventName);
    var handlerType = runtimeEvent.EventHandlerType;
    Func<T1, EventRegistrationToken> add = (a) => { return (EventRegistrationToken)runtimeEvent.AddMethod.Invoke(instance, new object[] { a }); };
    Action<EventRegistrationToken> remove = (a) => { runtimeEvent.RemoveMethod.Invoke(runtimeEvent, new object[] { a }); };

    WindowsRuntimeMarshal.AddEventHandler<T1>(add, remove, handler);
}

Ark-kun answer is correct because the method is decorated with Security Critical Attribute it can not be reflected by normal means. This again to protect the platform.

As he said the security considerations for reflection explains everything.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top