简体   繁体   中英

Visual Studio 'find all references' fails for event handlers?

Here is some simple sample C# code in Visual Studio 2008:

public partial class Form1 : Form
{
    private static System.Timers.Timer TestTimer;
    public Form1()
    {
        InitializeComponent();

        TestTimer = new System.Timers.Timer();
        TestTimer.Elapsed += DoSomething;            
    }

   private void DoSomething(Object source, EventArgs e)
   {   

   }
}

If I right click on the DoSomething assigned as a handler, and select Go to definition , VS finds the body of DoSomething . So far so good.

If I Right Click on it and Find all references it finds nothing. (!?)

If I do either of these actions for the body of DoSomething itself, it finds only itself, not the assignment as an event handler.

Am I missing something obvious? A setting perhaps? In all other cases when you ask for all references that includes the definition and every other reference. I realize the assignment is thinking in terms of delegates, but this seems inconsistent. It would be very convenient to easily find when something was assigned as a handler.

Your event handler declaration is not quite up to snuff. The ElapsedEventHandler delegate has a different signature. Fix:

    void DoSomething(object sender, System.Timers.ElapsedEventArgs e) {
       // etc..
    }

IntelliSense now will be able to find all references. Do favor using IntelliSense to get the event assignment correct. After you type += , press the Tab key twice to let it automatically generate the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM