共用方式為


HOW TO:使用 BuildManager 和 BuildManagerEvents 物件

BuildManager 物件是用來管理及顯示在執行產生設計階段輸出之自訂工具 (單一檔案產生器) 時所產生的可攜式執行檔 (Portable Executable,PE)。 當產生暫存可攜式執行檔的專案項目受到變更或刪除時,就會引發 BuildManagerEvents 事件。

下面會詳細說明如何在 Visual Studio 增益集中針對 BuildManagerBuildManagerEvents 物件設計程式。

注意事項注意事項

根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。 使用 [一般開發設定] 開發了這些程序。 若要變更設定,請從 [工具] 功能表中選取 [匯入和匯出設定]。 如需詳細資訊,請參閱 使用設定

若要使用 BuildManager 和 BuildManagerEvents 物件

  1. 使用 Visual C# 建立 Visual Studio 增益集專案。

  2. 在 [專案] 功能表上按一下 [加入參考],再按一下 [.NET] 索引標籤,選取 [System.Windows.Forms]、[VSLangProj]、[VSLangProj2] 和 [VSLangProj80],然後再按 [確定]。

  3. 將下列 using 陳述式加入至 Connect.cs 檔的頂端。

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    using System.Windows.Forms;
    
  4. 將下列宣告加入至 Connect 類別的最下方,以便宣告 BuildManagerEvents 處理常式。

    private DTE2 _applicationObject;
    private AddIn _addInInstance;
    private VSLangProj.BuildManagerEvents buildMgrEvents;
    
  5. 將下列方法呼叫加入至 OnConnection 方法。

    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    // Call the BuildMangerSample method.
    BuildManagerSample(_applicationObject);
    
  6. 緊接在 OnConnection 方法的下面加入 BuildManagerSample 方法宣告。

    public void BuildManagerSample(DTE2 dte)
    {
    }
    
  7. 將下列宣告加入至 BuildManagerSample 方法的最上方。

    Solution2 soln = (Solution2)_applicationObject.Solution;
    Project proj;
    VSProject2 vsproj;
    BuildManager bldMgr;
    
  8. 將下列程式碼加入至 BuildManagerSample 方法,將專案轉換為 VSProject2 物件。

    proj = soln.Projects.Item(1);
    // Get a reference to the VSProject2 object.
    vsproj = (VSProject2)proj.Object;
    
  9. 加入程式碼,使用 BuildDesignTimeOutput 將 PE 檔的 Moniker 顯示在訊息方塊中。

    bldMgr = vsproj.BuildManager;
    Array monikers = null;
    String msg = null;
    Object obj = bldMgr.DesignTimeOutputMonikers;
    if (obj != null)
    {
        try
        {
            monikers = (System.Array)obj;
            foreach(String tempmoniker in monikers)
                {
                    msg += bldMgr.BuildDesignTimeOutput(tempmoniker) 
    + "\n";
                }
            }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    MessageBox.Show("The build design-time output is:" + "\n"  
    + msg, "Temporary PE Monikers");
    }
    
  10. 將下列程式碼加入至 BuildManagerSample 方法,建立 BuildManagerEvents 事件處理常式。

    //Hook up buildmanager events.
    buildMgrEvents = vsproj.Events.BuildManagerEvents;
    buildMgrEvents.DesignTimeOutputDeleted +=
    new _dispBuildManagerEvents_DesignTimeOutputDeletedEventHandler
    (buildMgrEvents_DesignTimeOutputDeleted);
    buildMgrEvents. DesignTimeOutputDirty +=
    new _dispBuildManagerEvents_DesignTimeOutputDirtyEventHandler(
    buildMgrEvents_DesignTimeOutputDirty);
    
  11. BuildManagerEvents 物件的每個相關事件加入程序。

    void buildMgrEvents_DesignTimeOutputDirty
    (string bstrOutputMoniker)
    {
        MessageBox.Show(bstrOutputMoniker + " is dirty."
    , "BuildManager Events");
    }
    
    void buildMgrEvents_DesignTimeOutputDeleted
    (string bstrOutputMoniker)
    {
        MessageBox.Show(bstrOutputMoniker + " was deleted."
    , "BuildManager Events");
    }
    
  12. 最後,請將下列程式碼加入至 OnDisconnection 方法,停用事件處理功能。

    public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
    {
        // If the delegate handlers have been connected, then 
        // disconnect them here. 
        // If you do not do this, the handlers may still 
        // fire because garbage collection has not removed them.
        if (buildMgrEvents != null)
        {
            buildMgrEvents.DesignTimeOutputDeleted -=
     new _dispBuildManagerEvents_DesignTimeOutputDeletedEventHandler
    (buildMgrEvents_DesignTimeOutputDeleted);
            buildMgrEvents.DesignTimeOutputDirty -=
     new _dispBuildManagerEvents_DesignTimeOutputDirtyEventHandler
    (buildMgrEvents_DesignTimeOutputDirty);
        }
    }
    

    完整的程式碼會列在本主題的<範例>一節中。

  13. 若要建置增益集,請按一下 [建置] 功能表上的 [建置方案]。

  14. 在 Visual Studio 整合式開發環境 (IDE) 中開啟 Visual C# 或 Visual Basic 專案。

  15. 若要在專案中加入資料集,請按一下 [專案] 功能表上的 [加入新項目]。 從 [加入新項目] 對話方塊中選取 [資料集],然後按一下 [確定]。

    資料集檔案可確保專案具有相關聯的自訂工具 (單一檔案產生器)。

按一下 [工具] 功能表上的 [增益集管理員],然後從 [增益集管理員] 對話方塊中選取增益集。 按一下 [確定],執行您的增益集。

若要測試 BuildManagerEvents 程式碼

  • 若要查看 BuildManagerEvents 處理常式的引發狀況,請在專案中加入新的資料集、修改資料集檔案的屬性,或是刪除資料集檔案。

    若要修改資料集檔案的屬性:

    1. 在 [方案總管] 中選取資料集檔案。

    2. 以滑鼠右鍵按一下檔案,然後從下拉式功能表 (Drop-Down Menu) 中選取 [屬性]。

    3. 在 [屬性] 視窗中修改任何欄位。

    若要刪除資料集:

    1. 在 [方案總管] 中選取資料集檔案。

    2. 以滑鼠右鍵按一下檔案,然後從下拉式功能表中選取 [刪除]。

範例

下列是基本 Visual Studio 增益集的範例,在此範例中會示範如何藉由 Visual Studio Automation 來使用 BuildManagerBuildManagerEvents 物件。

using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
using System.Windows.Forms;
namespace MyAddIn
{
public class Connect : Object, IDTExtensibility2
    {
        public Connect()
        {
        }
        public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            _applicationObject = (DTE2)application;
            _addInInstance = (AddIn)addInInst;
            // Call the BuildMangerSample method.
            BuildManagerSample(_applicationObject);
        }
        public void BuildManagerSample(DTE2 dte)
        {
            try
            {
                Solution2 soln =
 (Solution2)_applicationObject.Solution;
                Project proj;
                VSProject2 vsproj;
                BuildManager bldMgr;
                proj = soln.Projects.Item(1);
                // Cast to the VSProject2 object.
                vsproj = (VSProject2)proj.Object;
                bldMgr = vsproj.BuildManager;
                Array monikers = null;
                String msg = null;
                Object obj = bldMgr.DesignTimeOutputMonikers;
                if (obj != null)
                {
                    try
                    {
                        monikers = (System.Array)obj;
                        foreach(String tempmoniker in monikers)
                        {
                            msg +=
 bldMgr.BuildDesignTimeOutput(tempmoniker) + "\n";
                        }
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    MessageBox.Show("The build design-time output is:"
+ "\n"  + msg, "Temporary PE Monikers");
                }
                //Hook up buildmanager events.
                buildMgrEvents = vsproj.Events.BuildManagerEvents;
                buildMgrEvents.DesignTimeOutputDeleted +=new
 _dispBuildManagerEvents_DesignTimeOutputDeletedEventHandler
(buildMgrEvents_DesignTimeOutputDeleted);
                buildMgrEvents.DesignTimeOutputDirty +=new
 _dispBuildManagerEvents_DesignTimeOutputDirtyEventHandler
(buildMgrEvents_DesignTimeOutputDirty);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void buildMgrEvents_DesignTimeOutputDirty
(string bstrOutputMoniker)
        {
            MessageBox.Show(bstrOutputMoniker + " is dirty.", 
"BuildManager Events");
        }
        void buildMgrEvents_DesignTimeOutputDeleted(
string bstrOutputMoniker)
        {
            MessageBox.Show(bstrOutputMoniker + " was deleted."
, "BuildManager Events");
        }
        public void OnDisconnection(ext_DisconnectMode disconnectMode
, ref Array custom)
        {
            // If the delegate handlers have been connected, then 
            // disconnect them here. 
            // If you do not do this, the handlers may still 
            // fire because garbage collection has not removed them.
            if (buildMgrEvents != null)
            {
                buildMgrEvents.DesignTimeOutputDeleted -= new
 _dispBuildManagerEvents_DesignTimeOutputDeletedEventHandler
(buildMgrEvents_DesignTimeOutputDeleted);
                buildMgrEvents.DesignTimeOutputDirty -= new
 _dispBuildManagerEvents_DesignTimeOutputDirtyEventHandler
(buildMgrEvents_DesignTimeOutputDirty);
            }
        }
        public void OnAddInsUpdate(ref Array custom)
        {
        }
        public void OnStartupComplete(ref Array custom)
        {
        }
        public void OnBeginShutdown(ref Array custom)
        {
        }
        private DTE2 _applicationObject;
        private AddIn _addInInstance;
        private VSLangProj.BuildManagerEvents buildMgrEvents;
    }
}
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports VSLangProj
Imports VSLangProj2
Imports VSLangProj80

Public Class Connect
    Implements IDTExtensibility2
    Dim _applicationObject As DTE2
    Dim _addInInstance As AddIn
    Public WithEvents buildMgrEvents As VSLangProj.BuildManagerEvents
    Public Sub New()
    End Sub
    Public Sub OnConnection(ByVal application As Object, ByVal _
    connectMode As ext_ConnectMode, ByVal addInInst As Object, _
    ByRef custom As Array) Implements IDTExtensibility2.OnConnection
        _applicationObject = CType(application, DTE2)
        _addInInstance = CType(addInInst, AddIn)
        BuildManagerSample(_applicationObject)
    End Sub
    Sub BuildManagerSample(ByVal dte As DTE2)
        Try
            Dim soln As Solution2 = CType(_applicationObject.Solution _
            , Solution2)
            Dim proj As Project
            Dim vsproj As VSProject2
            Dim bldMgr As BuildManager
            proj = soln.Projects.Item(1)
            ' Cast the project to a VSProject2.
            vsproj = CType(proj.Object, VSProject2)
            bldMgr = vsproj.BuildManager
            Dim monikers As String() = Nothing
            Dim moniker As String = Nothing
            Dim msg As String = ""
            Dim obj As Object = bldMgr.DesignTimeOutputMonikers
            If Not obj Is Nothing Then
                Try
                    monikers = CType(obj, String())
                    For Each moniker In monikers
                        msg &= bldMgr.BuildDesignTimeOutput(moniker)  _
                        + vbCr
                    Next
                Catch ex As System.Exception
                    MsgBox(ex.ToString)
                End Try
                MsgBox("The build design-time output is:" + vbCr  _
                + msg, MsgBoxStyle.Information _
                , "BuildManager Monikers")
            End If
            buildMgrEvents = vsproj.Events.BuildManagerEvents
            AddHandler buildMgrEvents.DesignTimeOutputDeleted _
            , AddressOf OutputDeleted
            AddHandler buildMgrEvents.DesignTimeOutputDirty _
            , AddressOf OutputDirty
        Catch ex As System.Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Sub OutputDeleted(ByVal deletedMoniker As String)
        MsgBox(deletedMoniker & " was deleted." _
        , MsgBoxStyle.Information, "BuildManagerEvents Information")
    End Sub
    Sub OutputDirty(ByVal dirtyMoniker As String)
        MsgBox(dirtyMoniker & " is dirty." _
        , MsgBoxStyle.Information, "BuildManagerEvents Information")
    End Sub
    Public Sub OnDisconnection(ByVal disconnectMode  _
    As ext_DisconnectMode, ByRef custom As Array)  _
    Implements IDTExtensibility2.OnDisconnection
        ' Turns off BuildManager event handling when the 
        ' add-in shuts down.
        buildMgrEvents = Nothing
    End Sub
    Public Sub OnAddInsUpdate(ByRef custom As Array)  _
    Implements IDTExtensibility2.OnAddInsUpdate
    End Sub
    Public Sub OnStartupComplete(ByRef custom As Array)  _
    Implements IDTExtensibility2.OnStartupComplete
    End Sub
    Public Sub OnBeginShutdown(ByRef custom As Array)  _
    Implements IDTExtensibility2.OnBeginShutdown
    End Sub
End Class

編譯程式碼

若要編譯這個程式碼,請建立新的 Visual Studio 增益集專案,並以範例中的程式碼取代 Connect 類別的程式碼。 在執行增益集之前,在 Visual Studio IDE 中開啟 Visual C# 或 Visual Basic 專案。 如需如何執行增益集的詳細資訊,請參閱 HOW TO:使用增益集管理員來控制增益集

請參閱

概念

BuildManager 物件簡介

專案擴充性簡介

其他資源

Visual Studio 的 Automation 和擴充性