Question

I am implementing Visual Studio Add-in and I want my service to be notified when class or method is removed in C# editor. Is there any samples showing how to do that ?

Was it helpful?

Solution

You're probably looking for code model events exposed via the DTE2 object. For fully featured synchronization you'll need to handle ElementChanged and ElementAdded events as well:

public void RegisterCodeModelEvents(DTE2 applicationObject)
{
   events = (Events2)applicationObject.Events; //events Must be a field
   codeModelEvents = events.get_CodeModelEvents(null); 

   codeModelEvents.ElementChanged += CodeModelElementChanged;
   codeModelEvents.ElementAdded += CodeModelElementAdded;
   codeModelEvents.ElementDeleted += CodeModelElementDeleted; // this is it!
}

Don't forget to remove the handlers after you finish:

private void UnregisterCodeModelEvents()
{
    if (codeModelEvents != null)
    {
        codeModelEvents.ElementAdded -= CodeModelElementAdded;
        codeModelEvents.ElementChanged -= CodeModelElementChanged;
        codeModelEvents.ElementDeleted -= CodeModelElementDeleted;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top