Partager via


How to: Compile and Run the Automation Object Model Code Examples

In Visual Studio 2013, add-ins are deprecated. We recommend that you upgrade your add-ins to VSPackage extensions. For more information about how to upgrade, see FAQ: Converting Add-ins to VSPackage Extensions.

The code examples found in the Automation object model reference topics are designed to run in an add-in created by the Add-in Wizard. The following sections explain how to compile and run the Automation object model code examples.

To run a code sample in an add-in

  1. Create an add-in by using the Add-in Wizard.

    For more information, see How to: Create an Add-In.

  2. Add the code example to the add-in's Connect class.

  3. Call the code example procedure from the add-in's OnConnection method. If the code example requires the DTE2 object as an argument, pass in the _applicationObject member variable, which is declared in the add-in code. The following code shows the relevant portions of the Connect class from both a Visual Basic and Visual C# add-in:

    Public Sub OnConnection(ByVal application As Object, ... ) _
      Implements IDTExtensibility2.OnConnection
    
        _applicationObject = CType(application, EnvDTE80.DTE2)
            .
            .
            .
        ' Pass the _applicationObject member variable to the code 
        ' example.
        MyCodeExample(_applicationObject)
    End Sub
    ' Other methods in the Connect class.
        .
        .
        .
    
    ' This is the code example that was added to the Connect class.
    Sub MyCodeExample(ByVal dte As DTE2)
        .
        .
        .
    End Sub
    
    public void OnConnection(object application, ... , ref Array custom)
    {
        _applicationObject = (DTE2)application;
            .
            .
            .
        // Pass the _applicationObject member variable to the code 
        // example.
        MyCodeExample(_applicationObject);
    }
    
    
    // Other methods in the Connect class.
        .
        .
        .
    
    // This is the code example that was added to the Connect class.
    void MyCodeExample(DTE2 dte)
    {
        .
        .
        .
    }
    
  4. For Visual C# add-ins, add a reference to the System.Windows.Forms assembly by opening the shortcut menu for the add-in project in Solution Explorer, choosing Add, References, choosing the Add New Reference button, and selecting System.Windows.Forms.dll in the Add Reference dialog box.

    1. For Visual C# add-ins, insert the following code at the top of the Connect.cs file:

      using System.Windows.Forms;
      
  5. Compile the add-in by selecting Build Solution from the Build menu.

  6. Select Add-in Manager from the Tools menu.

    The Add-in Manager dialog box appears.

  7. Load the add-in by selecting the check box next to the add-in's name in the Add-in Manager dialog box, and click OK.

See Also

Tasks

How to: Create an Add-In

How to: Control Add-Ins By Using the Add-In Manager

Concepts

Add-In Registration