views:

16

answers:

0

I want to invoke all registered methods of a RoutedEvent of a FrameworkElement (e.g. a MouseLeftButtonUp or a MouseMove of a ContentControl/Canvas/...). In my case, i got a reference to that object and I know which Event should be invoked. But after trying several hours with reflection I am asking myself if this is even possible???

I thought doing something like this:

var eventInfo = contentControl.GetType().GetEvent("MouseLeftButtonUp", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var getInvocationListMethod = eventInfo.EventHandlerType.GetMethod("GetInvocationList", BindingFlags.Instance | BindingFlags.Public);
Delegate[] delegates = (Delegate[])getInvocationListMethod.Invoke(eventInfo.EventHandlerType, null);
foreach (var handler in delegates)
{
    handler.Method.Invoke(handler.Target, new object[] { contentControl, new EventArgs()});
}

this actually always throws an exception with Message "Object does not match target type.". So has anybody a clue on this?

Cheers Tim