Breaking Down the Interfaces
Writing add-ins for Visual Studio .NET will involve two interfaces: IDTExtensibility2, contained with the Extensibility namespace provided by Extensibility.dll
, and IDTCommandTarget, contained with the EnvDTE namespace provided by EnvDTE.dll
. The members exposed by these interfaces will expose your add-in's primary communication channel with the IDE during setup, and if you are declaring named commands, during runtime upon invocation of your named commands.
Providing Core IDE Communication
The IDTExtensibility2interface implemented by an add-in is the same interface you used to write COM add-ins for Office XP. For some readers this will provide a welcome feeling of familiarity. You use this interface to provide a communication mechanism between the IDE and your code. This interface contains five method members, with two primary methods used to signal the loading and unloading of your add-in code.
You'll start this communication with the OnConnection
method. Within this method, Visual Studio .NET will provide an object reference to the IDE itself. You should cast the initial object parameter passed into the method into the core type provided by the EnvDTE
Dim IDE As EnvDTE.DTE
Public Sub OnConnection( _
ByVal Application As Object, _
ByVal ConnectMode As ext_ConnectMode, _
ByVal AddInInst As Object, _
ByRef custom As System.Array) _
IDE = CType(Application, EnvDTE.DTE)
The primary actions performed by most add-ins involve access and manipulation of objects contained within the EnvDTE namespace. The instance of the DTE object passed into OnConnection
will provide you this ability. The second parameter will provide instruction to your add-in regarding the reason the add-in was loaded. The second parameter uses the enumeration type ext_ConnectMode
found in the Extensibility namespace. You should use either an If...Then
statement to provide behavior appropriate for each add-in loading scenario. For a simple add-in, you will most likely limit the code in OnConnection
to setting up your initial UI. The third parameter provides an object reference to your add-in's own object instance, of type EnvDTE.AddIn,
and the final parameter will generally be of limited value to you. Currently, the IDE passes an empty array for the final parameter of the OnConnection
The second most important method is the OnDisconnection
method. This method indicates the end of an add-in's life. The add-in architecture in Visual Studio .NET will result in the add-in class being loaded at least twice. The removeMode
parameter uses the enumeration type ext_DisconnectMode
found in the Extensibility namespace. You should use the removeMode
parameter to respond appropriately for the scenario in which your add-in was unloaded. You should use this method for any required clean up proceedings.
The final three methods of the IDTExtensibility2
interface will normally be of limited use. Use OnStartupComplete
when your initialization code, normally placed in the OnConnection
method, needs to access IDE components. The IDE automation model objects you access, found in the EnvDTE namespace, may not yet be available; therefore you should place initialization code in OnStartupComplete
to compensate for IDE component dependencies in your add-in initialization routines.
method will interrupt your add-in to inform your code that another add-in has either been loaded or unloaded. Use this method to respond to a changing add-in landscape. Do not get your hopes up about the direct usability of this method to adjust to add-in dependency scenarios; only one parameter is provided and this parameter provides no information as to the source or nature of the change in add-in landscape.
is called only if Visual Studio .NET attempts to shutdown while an add-in is running. Shutdown at this point is irreversible and this method should be used only to clean up items prior to the inevitable IDE shutdown.