Components of an Add-in Project
Add-in projects are Class Library projects that are created by using the Add-in Wizard and that are compiled into DLLs. Add-in projects contain a source code file named Connect, which is also the name of the Class. The Connect class implements an interface named IDTExtensibility2 which passes commands between the add-in and the Visual Studio integrated development environment (IDE).
IDTExtensibility2 has five methods that, when implemented, act as events.
This method is called when the add-in is loaded in Visual Studio.
This method is called when Visual Studio finishes loading.
This method is called when an add-in loads or unloads from Visual Studio.
This method is called when Visual Studio is closed.
This method is called when the add-in is unloaded from Visual Studio.
In addition to the IDTExtensibility2 interface, the IDTCommandTarget interface is automatically implemented if you check the user interface option when using the Add-in Wizard to create an add-in. If you choose to create or manipulate command bars in your add-in, you must also implement the namespace Microsoft.VisualStudio.CommandBars.
The OnConnection method is definitely the most important method used in add-in projects because it is called each time an add-in is loaded. Furthermore, it is used to call other automation code in the add-in. The OnConnection method is passed four parameters: Application, ConnectMode, AddInInst, and custom. Application represents the Visual Studio IDE. It is cast as a DTE2 object with the name, _applicationObject. This object represents the main object in the core automation model and provides access to all of its types and members. ConnectMode (whose values are contained in Extensibility.extConnectMode) represents the way in which the add-in is being loaded; that is, through the command line, by opening a solution, and so forth. AddInInst represents the add-in itself. The custom parameter is an array in which you can optionally pass data to the add-in.
In addition to initializing these variables, OnConnection also contains code to create a command for the add-in on the Tools menu if you selected that option when creating it with the Add-in Wizard.
The other four add-in methods, which are put in place by the Add-in Wizard, are empty by default. To handle add-in related events, you can use these other methods to respond to them. For example, you could add code to the OnAddInsUpdate method to send a notification message to another procedure when an add-in is closed. You can call OnBeginShutdown to perform cleanup tasks when the Visual Studio IDE is being shut down.
When you create an add-in and check the "Would you like to create a command bar UI for your Add-in?" option (which creates a command for the add-in on the Tools menu), the IDTCommandTarget interface is implemented. Two additional methods — QueryStatus and Exec — are added to the add-in project to handle the command tasks. These methods contain a small amount of code to help place the command on the Tools menu and respond to clicks from a user. QueryStatus notifies the add-in of the command's availability. The Exec method is called when a user clicks the add-in's command on the Tools menu, so this is where you should add code if you want to respond to that event.