4

I am trying to subscribe to ALL events exposed by a WPF GridView-like 3rd party component in order to do some debugging. Aside from the suggestion that this might not be the best way to go about debugging it and stuff like that I would like to know if this can be done.

For the routed events it worked ok like this :

var type = tree.GetType();
do
{
    var staticFields = type.GetFields(BindingFlags.Static | BindingFlags.Public);
    foreach (var staticField in staticFields)
    {
        if (typeof(RoutedEvent).IsAssignableFrom(staticField.FieldType))
        {
            tree.AddHandler((RoutedEvent)staticField.GetValue(null), new RoutedEventHandler(OnRoutedEvent), true);
        }
    }
} while ((type = type.BaseType) != typeof(object)/* && type.FullName.StartsWith("Telerik")*/);

public void OnRoutedEvent(object sender, System.Windows.RoutedEventArgs e)
{
    Debug.WriteLine(e.RoutedEvent.ToString());
}

However, with typical events this doesn't seem to work :

var evts = tree.GetType().GetEvents();
foreach (var ev in evts)
{
    ev.AddEventHandler(this, new EventHandler(OnEvent));
}

public void OnEvent(object sender, EventArgs e)
{
      //..
}

because it doesn't like either the thing that the delegate is EventHandler instead of the specialized type or because the signature of the event handler method does not contain the specialized EventArgs class type.

Can this be done somehow?

------------ LATER EDIT --------- In all three cases (my attempt, ds27680's suggestion and Thomas Levesque's suggestion) the AddEventHandler call fails with :

        System.Reflection.TargetException occurred
            Message=Object does not match target type.
            Source=mscorlib
                StackTrace:
                   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
                   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
                   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
                   at System.Reflection.EventInfo.AddEventHandler(Object target, Delegate handler)
                   at Test.MainWindow..ctor() in c:\users\me\documents\visual studio 2010\Projects\Test\Test\MainWindow.xaml.cs:line 39

I guess the fact that the event handler method's signature does not match EXACTLY the EventArgs type is what makes it fail...

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166

3 Answers3

3

You need to "convert" the delegate to the appropriate type:

var evts = tree.GetType().GetEvents();
EventHandler tmp = OnEvent;
foreach (var ev in evts)
{
    Delegate handler = Delegate.CreateDelegate(ev.EventHandlerType, tmp.Target, tmp.Method);
    ev.AddEventHandler(this, handler);
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Just like my initial attempt : System.Reflection.TargetException : {"Object does not match target type."} – Andrei Rînea Mar 28 '11 at 10:36
  • I tried it and it worked fine for me... Anyway, it's not like your code: you were trying to subscribe the handler directly, without changing the delegate type – Thomas Levesque Mar 28 '11 at 11:38
  • Yes, the problem, as I was telling ds27680, was from the control. It had events that do not follow the (object sender, EventArgs args) pattern. – Andrei Rînea Mar 28 '11 at 11:40
2

You can construct events of a specific type using Delegate.CreateDelegate. Worth a try.

The Fiddler
  • 2,726
  • 22
  • 28
2

You could try:

public class DebugHook
{
    public static void OnEvent<EventArgsType>(object sender, EventArgsType eventArgs)
    {

    }
}

then:

foreach (var ev in evts)
{
   Type argsType = getEventArgsType(ev);

   MethodInfo hook = typeof(DebugHook).GetMethod("OnEvent");
   MethodInfo boundEventhandler = hook.MakeGenericMethod(new [] { argsType} );

   Delegate handler = Delegate.CreateDelegate(ev.EventHandlerType, boundEventhandler);

   ev.AddEventHandler(this, handler );
}

where getEventArgs looks like this:

 public Type getEventArgsType(EventInfo eventType)
 {
     Type t = eventType.EventHandlerType;
     MethodInfo m = t.GetMethod("Invoke");

     var parameters = m.GetParameters();
     return parameters[1].ParameterType;
 }

Of course a lot of error checking/handling is missing...

ds27680
  • 1,993
  • 10
  • 11
  • Just like my initial attempt : System.Reflection.TargetException : {"Object does not match target type."} – Andrei Rînea Mar 28 '11 at 10:46
  • @Andrei Rinea That's strange, I tested with a WPF app and a Grid control and it works... – ds27680 Mar 28 '11 at 10:55
  • @Andrei Rinea: Can it be the custom control you are using has some custom events that do not follow the (object sender, EventArgs args) pattern? Did you try with a standard WPF control? You could try to trace out the parameter count and type for each event in the getEventArgsType method... – ds27680 Mar 28 '11 at 11:01
  • Well I've tested it with a Grid (the plain layout container) and it worked. In fact you're right. The Telerik's RadTreeListView has a PreviewDragEnded event where the code fails. This event is of type RadTreeListViewDragEndedEventHandler which is defined as : public delegate void RadTreeListViewDragEndedEventHandler(object sender, RadTreeListViewDragEndedEventArgs e); – Andrei Rînea Mar 28 '11 at 11:05
  • 1
    Apparently RadTreeListViewDragEndedEventArgs inherits EventArgs, so it shouldn't be an issue... – Thomas Levesque Mar 28 '11 at 12:07
  • @Thomas Levesque : I've also looked into the inheritance tree of that damn Rad...EventArgs class and it does inherit from System.EventArgs but it seems that covariance doesn't really work here.. – Andrei Rînea Mar 28 '11 at 14:54
  • @Andrei Rinea I think the example I posted should work even if RadTreeListViewDragEndedEventArgs does not inherit EventArgs. As you see I am using a generic method here that takse two parameters (the first one is Object the second one will be RadTreeListViewDragEndedEventArgs in that case. So it should work. I think however your (an also mines due to copy paste :-) ) mistake is that we do: ev.AddEventHandler(this, handler ); while in the main frame. I think correctly in your case would be: ev.AddEventHandler(tree, handler );... – ds27680 Mar 28 '11 at 15:37
  • @Andrei Rinea The posted code will not work if there is an "exotic event with let's say three parameters... ;-) – ds27680 Mar 28 '11 at 15:41
  • Two important things about this example: 1. Notice that OnEvent is static in this example. If you want to use CreateDelegate with an instance method, you need to use a different overload of CreateDelegate. 2. The first argument to AddEventHandler, simply called "target" and not well-documented, means the object that declared the event, not the object that initiated the event nor the subscriber. – Chris Moschini Jan 18 '13 at 17:54