Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find list of events using Debugger (VS Professional 2012)?

Okay, I can't find any help for my question and stackoverflow doesn't seem to have anything either, or I didn't know how to look for it (please correct me, if I'm wrong and I will close this question).

In my program, I have a Grid which has a few events definded in code:

public Grid _grid = new Grid();
_grid.MouseLeftButtonDown += new MouseButtonEventHandler(MyMethod);
//and a few more events...

Now during my program run, I saw some weird behaviour that can only come from some events, so I set a breakpoint and stopped the program to use the debugger.

Is there a list I can find somewhere that lists all the currently defined events of the member _grid so I can check that no unwanted events have not yet been removed?

like image 908
philkark Avatar asked Feb 12 '13 18:02

philkark


1 Answers

Update 2

Unfortunately, most events in WPF (i.e. on UIElement) are implemented by manually implementing add/remove which means the event member can only be on the left hand side of a -= or += operator (i.e. it can't be "read"). The internals are such that each event is "delegated" to a collection of events and that collection only contains elements for assigned events (e.g. if there's a single MouseLeftButtonDownEvent += somehandler; then that collection of events will have only one entry. Unfortunately, what the collection of events stores to represent a handler is an internal structure that you would have to be able to instantiate to query the collection. You are unable to instantiate an instance of that structure (RoutedEventHandlerInfo, FWIW) in order to query the collection (UIElement.EventHandlersStore._entries, also FWIW). e.g. if you could you, you could query the handler for a particular event as such in the QuickWatch window:

grid.EventHandlersStore._entries[
    new RoutedEventHandlerInfo(UIElement.MouseLeftButtonDownEvent, false)]

But, the debugger does not allow you to invoke an internal constructor.

There isn't something that lists just the events. You can see all the members of a instance in the debugger (watch, quickwatch, etc.) and the events have a distinct icon. You can then expand each one of these to see what method was assigned to the event. For example:

enter image description here

As you can see, MyEvent has been "assigned" the method t_MyEvent for this particular instance.

Update: If you have more than one event handler assigned to an event, the debugger will only show the last assigned method in top-level of the event in quick watch. To see all the methods assigned, you'll need to drill-down to the invocation list. For example:

enter image description here

.. this shows that both t_MyEvent and t_MyEvent2 are in the invocation list for MyEvent. If you hace no handlers, the value for MyEvent will be null.

like image 74
Peter Ritchie Avatar answered Sep 28 '22 12:09

Peter Ritchie