Freigeben über


Gewusst wie: Kompilieren und Ausführen der Codebeispiele für das Automatisierungsobjektmodell

Aktualisiert: November 2007

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. In addition, the Visual Basic code examples can run as a macro. 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 Gewusst wie: Erstellen von Add-Ins.

  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 clicking the add-in project's name in Solution Explorer, selecting Add Reference from the Project menu, and choosing 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.

To run a Visual Basic code example as a macro

  1. On the Tools menu, point to Macros, and then click Macros IDE.

    The Macros IDE appears.

  2. On the View menu of the Macros IDE, click Project Explorer.

  3. Expand the MyMacros node in Project Explorer.

  4. Double-click Module1.

  5. Add the code example to the module file.

  6. If the code example requires the DTE object as an argument, create a macro without parameters that passes the global DTE variable to the code example. The following code shows how:

    ' Visual Basic
    ' Macro without parameters that calls the code example.
    Sub CodeExample()
        CodeExample(DTE2)
    End Sub
    
    ' Original code example.
    Sub CodeExample(ByVal dte As DTE2)
        .
        .
        .
    End Sub
    
  7. Close the Macros IDE.

  8. On the Tools menu in the Visual Studio IDE, point to Macros, and then click Macro Explorer.

    The Macro Explorer window appears.

  9. In Macro Explorer, expand the MyMacros node, and then expand the Module1 node.

  10. In Macro Explorer, right-click the code example macro name and select Run from the shortcut menu.

Siehe auch

Aufgaben

Gewusst wie: Erstellen von Add-Ins

Gewusst wie: Steuern von Add-Ins mit dem Add-In-Manager

Gewusst wie: Bearbeiten und programmgesteuertes Erstellen von Makros

Konzepte

Add-In-Registrierung