2

I am quite new to WPF and rather new to C# and now have inherited a C#/WPF App with over 60k lines of code (no MVVM was used, the design pattern is rather improvised). Now, I have to change a rather small thing in the UI, but this control is quite crucial and therefore heavily woven into the whole solution. I know the "Live Visual Tree" Function in Visual Studio and also I know the Live Property Explorer. However, I would like to have something like a "Live EVENT Explorer" to see what methods are registered to an event of this control. I would like to have something like:

a) A possibility to see, what methods are currently subscribed to a certain event (for example the SelectionChanged event of a ListView). With "currently" I mean, that the subscribed methods are changing quite a lot depending on what radio buttons and check boxes are selected, etc. So I would like to make all the selections I need and then inspect the control and see what methods are subscribed to one of its events.

b) A possibility to stop right in the first method that is being triggered by this event if I for example change the selection in my control. So that I can step through all the following code?

Does anyone of you have an idea if either one of this is possible with a built-in function in Visual Studio and also how to do it? Or is there maybe another tool out there how to achieve something like this?

WurzelseppQX
  • 520
  • 1
  • 6
  • 17
  • Are you using MVVM? – Nekeniehl Nov 12 '18 at 15:11
  • Good question. This I should have mentioned. No MVVM was used. The design pattern is rather "improvised". Also the colleague who wrote most of it is not in the company anymore. – WurzelseppQX Nov 12 '18 at 15:13
  • See https://stackoverflow.com/questions/1191433/tool-to-trace-application-without-code-changes and https://stackoverflow.com/questions/3978364/debugging-all-events-in-visual-studio-2010-without-setting-break-points. – Sergey Vlasov Nov 13 '18 at 03:26
  • Any update for this issue? Have you resolved this issue? Does the answers below solve your problem?? – Joy Nov 15 '18 at 01:17
  • After some testing, the answer of Mitch fits my question best, but also the answer of Nekeniehl is a good one (i.e. combined with #IF Debug preprocessor instruction), depending on the situation. Also his comment about the Snoop Tool helped me a lot (in general). With all the answers and comments, I figured out a way of working which is fine for me for the moment. But originally I hoped that there was maybe a kind of tool where I can just inspect a WPF control and see all methods subscribed to an event, similar to Live Property viewer. But such a tools seems not to exist yet. – WurzelseppQX Nov 15 '18 at 08:09

1 Answers1

3

In most cases, Find All References should have it covered, but this fails when the event is not unique enough (imagine Button.Click).

You can access this in a debugger by browsing to the event object and examining the _invocationList field. If this field is not populated, look at _methodPtr field. If both fields are null, then no one is subscribed.

_target is the object containing the subscribed method. If it is null, a static method is subscribed (which makes identification much more tricky). Otherwise, you can dump the method table of the target object to find the subscribed method.

In Visual studio, the debug tooltips make this easy. For a unicast delegate, hovering over the event shows the declaring type and method name (and arity if needed):

screencap showing debugger tooltip

for multicast, the _invocationList takes over:

screencap showing debugger tooltip

Mitch
  • 21,223
  • 6
  • 63
  • 86