How to find all methods currently subscribed to an event in WPF / C# during debugging in Visual Studio












1















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?










share|improve this question

























  • 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 stackoverflow.com/questions/1191433/… and stackoverflow.com/questions/3978364/….

    – Sergey Vlasov
    Nov 13 '18 at 3:26











  • Any update for this issue? Have you resolved this issue? Does the answers below solve your problem??

    – Alex
    Nov 15 '18 at 1: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 8:09
















1















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?










share|improve this question

























  • 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 stackoverflow.com/questions/1191433/… and stackoverflow.com/questions/3978364/….

    – Sergey Vlasov
    Nov 13 '18 at 3:26











  • Any update for this issue? Have you resolved this issue? Does the answers below solve your problem??

    – Alex
    Nov 15 '18 at 1: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 8:09














1












1








1








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?










share|improve this question
















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?







c# wpf debugging visual-studio-debugging






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 15:25







WurzelseppQX

















asked Nov 12 '18 at 15:02









WurzelseppQXWurzelseppQX

282213




282213













  • 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 stackoverflow.com/questions/1191433/… and stackoverflow.com/questions/3978364/….

    – Sergey Vlasov
    Nov 13 '18 at 3:26











  • Any update for this issue? Have you resolved this issue? Does the answers below solve your problem??

    – Alex
    Nov 15 '18 at 1: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 8:09



















  • 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 stackoverflow.com/questions/1191433/… and stackoverflow.com/questions/3978364/….

    – Sergey Vlasov
    Nov 13 '18 at 3:26











  • Any update for this issue? Have you resolved this issue? Does the answers below solve your problem??

    – Alex
    Nov 15 '18 at 1: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 8:09

















Are you using MVVM?

– Nekeniehl
Nov 12 '18 at 15:11





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





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 stackoverflow.com/questions/1191433/… and stackoverflow.com/questions/3978364/….

– Sergey Vlasov
Nov 13 '18 at 3:26





See stackoverflow.com/questions/1191433/… and stackoverflow.com/questions/3978364/….

– Sergey Vlasov
Nov 13 '18 at 3:26













Any update for this issue? Have you resolved this issue? Does the answers below solve your problem??

– Alex
Nov 15 '18 at 1:17







Any update for this issue? Have you resolved this issue? Does the answers below solve your problem??

– Alex
Nov 15 '18 at 1: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 8:09





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 8:09












2 Answers
2






active

oldest

votes


















2














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






share|improve this answer

































    1














    You can go to the View where the ListView is, and in code-behind, subscribe your self to the ListView.SelectionChanged event and add the following code on the handling of the event:



    foreach(EventHandler subscriber in ListView.SelectionChanged.GetInvocationList())
    {
    // etc
    }


    You can get more info from here:



    How do I get the subscribers of an event?



    I hope that helps you a bit to debug it.






    share|improve this answer
























    • I will try that, however I hoped to have somehow a built-in function of VS or another tool that does not need some additional code (as I would have this case for many events). But for a single event, this is a nice solution.

      – WurzelseppQX
      Nov 12 '18 at 15:24











    • AFAIK there is non build-in function or external tool to get the subscribers of an event without stoping the execution and debuggint it.

      – Nekeniehl
      Nov 12 '18 at 15:27











    • After further investigation, I use a tool wich I didnt know you can check the events and by whom was handled it. Check SnoopWpf -> github.com/cplotts/snoopwpf/releases

      – Nekeniehl
      Nov 14 '18 at 10:25













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264861%2fhow-to-find-all-methods-currently-subscribed-to-an-event-in-wpf-c-sharp-during%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    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






    share|improve this answer






























      2














      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






      share|improve this answer




























        2












        2








        2







        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






        share|improve this answer















        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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 20:37

























        answered Nov 12 '18 at 15:30









        MitchMitch

        14.7k33663




        14.7k33663

























            1














            You can go to the View where the ListView is, and in code-behind, subscribe your self to the ListView.SelectionChanged event and add the following code on the handling of the event:



            foreach(EventHandler subscriber in ListView.SelectionChanged.GetInvocationList())
            {
            // etc
            }


            You can get more info from here:



            How do I get the subscribers of an event?



            I hope that helps you a bit to debug it.






            share|improve this answer
























            • I will try that, however I hoped to have somehow a built-in function of VS or another tool that does not need some additional code (as I would have this case for many events). But for a single event, this is a nice solution.

              – WurzelseppQX
              Nov 12 '18 at 15:24











            • AFAIK there is non build-in function or external tool to get the subscribers of an event without stoping the execution and debuggint it.

              – Nekeniehl
              Nov 12 '18 at 15:27











            • After further investigation, I use a tool wich I didnt know you can check the events and by whom was handled it. Check SnoopWpf -> github.com/cplotts/snoopwpf/releases

              – Nekeniehl
              Nov 14 '18 at 10:25


















            1














            You can go to the View where the ListView is, and in code-behind, subscribe your self to the ListView.SelectionChanged event and add the following code on the handling of the event:



            foreach(EventHandler subscriber in ListView.SelectionChanged.GetInvocationList())
            {
            // etc
            }


            You can get more info from here:



            How do I get the subscribers of an event?



            I hope that helps you a bit to debug it.






            share|improve this answer
























            • I will try that, however I hoped to have somehow a built-in function of VS or another tool that does not need some additional code (as I would have this case for many events). But for a single event, this is a nice solution.

              – WurzelseppQX
              Nov 12 '18 at 15:24











            • AFAIK there is non build-in function or external tool to get the subscribers of an event without stoping the execution and debuggint it.

              – Nekeniehl
              Nov 12 '18 at 15:27











            • After further investigation, I use a tool wich I didnt know you can check the events and by whom was handled it. Check SnoopWpf -> github.com/cplotts/snoopwpf/releases

              – Nekeniehl
              Nov 14 '18 at 10:25
















            1












            1








            1







            You can go to the View where the ListView is, and in code-behind, subscribe your self to the ListView.SelectionChanged event and add the following code on the handling of the event:



            foreach(EventHandler subscriber in ListView.SelectionChanged.GetInvocationList())
            {
            // etc
            }


            You can get more info from here:



            How do I get the subscribers of an event?



            I hope that helps you a bit to debug it.






            share|improve this answer













            You can go to the View where the ListView is, and in code-behind, subscribe your self to the ListView.SelectionChanged event and add the following code on the handling of the event:



            foreach(EventHandler subscriber in ListView.SelectionChanged.GetInvocationList())
            {
            // etc
            }


            You can get more info from here:



            How do I get the subscribers of an event?



            I hope that helps you a bit to debug it.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 '18 at 15:17









            NekeniehlNekeniehl

            593420




            593420













            • I will try that, however I hoped to have somehow a built-in function of VS or another tool that does not need some additional code (as I would have this case for many events). But for a single event, this is a nice solution.

              – WurzelseppQX
              Nov 12 '18 at 15:24











            • AFAIK there is non build-in function or external tool to get the subscribers of an event without stoping the execution and debuggint it.

              – Nekeniehl
              Nov 12 '18 at 15:27











            • After further investigation, I use a tool wich I didnt know you can check the events and by whom was handled it. Check SnoopWpf -> github.com/cplotts/snoopwpf/releases

              – Nekeniehl
              Nov 14 '18 at 10:25





















            • I will try that, however I hoped to have somehow a built-in function of VS or another tool that does not need some additional code (as I would have this case for many events). But for a single event, this is a nice solution.

              – WurzelseppQX
              Nov 12 '18 at 15:24











            • AFAIK there is non build-in function or external tool to get the subscribers of an event without stoping the execution and debuggint it.

              – Nekeniehl
              Nov 12 '18 at 15:27











            • After further investigation, I use a tool wich I didnt know you can check the events and by whom was handled it. Check SnoopWpf -> github.com/cplotts/snoopwpf/releases

              – Nekeniehl
              Nov 14 '18 at 10:25



















            I will try that, however I hoped to have somehow a built-in function of VS or another tool that does not need some additional code (as I would have this case for many events). But for a single event, this is a nice solution.

            – WurzelseppQX
            Nov 12 '18 at 15:24





            I will try that, however I hoped to have somehow a built-in function of VS or another tool that does not need some additional code (as I would have this case for many events). But for a single event, this is a nice solution.

            – WurzelseppQX
            Nov 12 '18 at 15:24













            AFAIK there is non build-in function or external tool to get the subscribers of an event without stoping the execution and debuggint it.

            – Nekeniehl
            Nov 12 '18 at 15:27





            AFAIK there is non build-in function or external tool to get the subscribers of an event without stoping the execution and debuggint it.

            – Nekeniehl
            Nov 12 '18 at 15:27













            After further investigation, I use a tool wich I didnt know you can check the events and by whom was handled it. Check SnoopWpf -> github.com/cplotts/snoopwpf/releases

            – Nekeniehl
            Nov 14 '18 at 10:25







            After further investigation, I use a tool wich I didnt know you can check the events and by whom was handled it. Check SnoopWpf -> github.com/cplotts/snoopwpf/releases

            – Nekeniehl
            Nov 14 '18 at 10:25




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264861%2fhow-to-find-all-methods-currently-subscribed-to-an-event-in-wpf-c-sharp-during%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Hercules Kyvelos

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud