Share via


HOW TO:編譯和執行 Automation 物件模型程式碼範例

Automation 物件模型參考主題中的程式碼範例,是設計在由 [增益集精靈] 所建立的增益集中執行。 此外,Visual Basic 程式碼範例也可以當做巨集執行。 下列各節將說明如何編譯和執行 Automation 物件模型程式碼範例。

若要在增益集中執行程式碼範例

  1. 使用 [增益集精靈] 建立增益集。

    如需詳細資訊,請參閱 HOW TO:建立增益集

  2. 將程式碼範例加入至增益集的 Connect 類別。

  3. 從增益集的 OnConnection 方法呼叫程式碼範例程序。 如果程式碼範例需要 DTE2 物件做為引數,請將增益集程式碼中所宣告的 _applicationObject 成員變數 (Member Variable) 傳遞至此引數。 下列程式碼顯示的是 Visual Basic 和 Visual C# 增益集中,Connect 類別的相關部分:

    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. 在 Visual C# 增益集中,加入 System.Windows.Forms 組件的參考,方式是:在 [方案總管] 中按一下增益集專案的名稱,從 [專案] 功能表中選取 [加入參考],然後在 [加入參考] 對話方塊中選擇 [System.Windows.Forms.dll]。

    1. 在 Visual C# 增益集中,將下列程式碼插入在 Connect.cs 檔案的最頂端位置:

      using System.Windows.Forms;
      
  5. 從 [建置] 功能表中選取 [建置方案],編譯增益集。

  6. 從 [工具] 功能表中選取 [增益集管理員]。

    [增益集管理員] 對話方塊隨即出現。

  7. 在 [增益集管理員] 對話方塊中,選取增益集名稱旁的核取方塊,再按 [確定],載入增益集。

若要將 Visual Basic 程式碼範例當做巨集執行

  1. 在 [工具] 功能表中,指到 [巨集],接著按一下 [巨集 IDE]。

    巨集 IDE 隨即出現。

  2. 巨集 IDE 的 [檢視] 功能表上,按一下 [專案總管]。

  3. 展開 [專案總管] 中的 [MyMacros] 節點。

  4. 按兩下 [Module1]。

  5. 將程式碼範例加入至此模組檔案。

  6. 如果程式碼範例需要 DTE 物件做為引數,請建立一個不含參數的巨集,將 DTE 全域變數傳遞給程式碼範例。 下列程式碼將說明進行步驟:

    ' Visual Basic
    ' Macro without parameters that calls the code example.
    Sub CodeExample()
        CodeExample(DTE)
    End Sub
    
    ' Original code example.
    Sub CodeExample(ByVal dte As DTE2)
        .
        .
        .
    End Sub
    
  7. 關閉巨集 IDE

  8. 在 Visual Studio IDE 的 [工具] 功能表上,指向 [巨集],然後按一下 [巨集總管]。

    [巨集總管] 視窗隨即出現。

  9. 在 [巨集總管] 中,展開 [MyMacros] 節點,再展開 [Module1] 節點。

  10. 在 [巨集總管] 中,以滑鼠右鍵按一下程式碼範例的巨集名稱,然後從捷徑功能表中選取 [執行]。

請參閱

工作

HOW TO:建立增益集

HOW TO:使用增益集管理員來控制增益集

HOW TO:編輯和以程式設計方式建立巨集

概念

增益集登錄