如何:使用 BuildManager 和 BuildManagerEvents 对象

BuildManager 对象用于管理和显示可迁移可执行 (PE) 文件,这些文件通过运行自定义工具(单个文件生成器)产生并生成设计时输出。 更改或删除生成临时可移植可执行文件的项目项时将引发 BuildManagerEvents 事件。

本文详细介绍了如何在 Visual Studio 外接程序中对 BuildManagerBuildManagerEvents 对象进行编程。

备注

以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。您安装的 Visual Studio 版本以及使用的设置决定了这些元素。有关详细信息,请参阅在 Visual Studio 中自定义开发设置

使用 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. 将 BuildManagerSample 方法声明添加到 OnConnection 方法的正下方。

    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 文件的名字对象。

    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. 右击该文件,并从下拉菜单中选择“属性”。

    3. 在**“属性”**窗口中修改任何字段。

    删除数据集:

    1. 在“解决方案资源管理器”中选择数据集文件。

    2. 右击该文件,并从下拉菜单中选择“删除”。

示例

下面的示例是一个基本 Visual Studio 外接程序,演示如何使用 Visual Studio 自动化来使用 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 项目。 有关如何运行外接程序的信息,请参见如何:使用外接程序管理器控制外接程序

请参见

概念

BuildManager 对象介绍

项目扩展性介绍

其他资源

Visual Studio 的自动化和扩展性