次の方法で共有


方法 : BuildManager オブジェクトおよび BuildManagerEvents オブジェクトを使用する

BuildManager オブジェクトは、デザイン時の出力を生成するカスタム ツール (単一ファイル ジェネレーター) を実行して作成された移植可能な実行可能 (PE: Portable Executable) ファイルを管理および表示するために使用します。BuildManagerEvents イベントは、一時的なポータブル実行可能 (PE) ファイルを生成するプロジェクト項目が変更または削除されたときに発生します。

Visual Studio アドインで BuildManager オブジェクトおよび BuildManagerEvents オブジェクトに対してプログラムを作成する方法の詳細を次に示します。

[!メモ]

実際に画面に表示されるダイアログ ボックスとメニュー コマンドは、アクティブな設定またはエディションによっては、ヘルプの説明と異なる場合があります。ここに記載されている手順は、全般的な開発設定が適用されているものとして記述されています。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。詳細については、「Visual Studio の設定」を参照してください。

BuildManager オブジェクトおよび BuildManagerEvents オブジェクトを使用するには

  1. Visual C# を使用して、Visual Studio アドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックし、[.NET] タブをクリックします。[System.Windows.Forms]、[VSLangProj]、[VSLangProj2]、および [VSLangProj80] を選択し、[OK] をクリックします。

  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. プロパティにデータセットを追加するには、[プロジェクト] メニューの [新しい項目の追加] をクリックします。[新しい項目の追加] ダイアログ ボックスの [データセット] を選択し、[OK] をクリックします。

    データセット ファイルでは、プロジェクトにカスタム ツール (単一ファイル ジェネレーター) が関連付けられていることを確認します。

[ツール] メニューの [アドイン マネージャー] をクリックし、[アドイン マネージャー] ダイアログ ボックスでアドインを選択します。[OK] をクリックしてアドインを実行します。

BuildManagerEvents コードをテストするには

  • BuildManagerEvents ハンドラーの発生を確認するには、新しいデータセットをプロジェクトに追加するか、データセット ファイルのプロパティを変更するか、データセット ファイルを削除します。

    データセット ファイルのプロパティを変更するには、次の手順を実行します。

    1. ソリューション エクスプローラーで、データセット ファイルを選択します。

    2. ファイルを右クリックし、ドロップダウン メニューの [プロパティ] を選択します。

    3. [プロパティ] ウィンドウで、いずれかのフィールドを変更します。

    データセットを削除するには、次の手順を実行します。

    1. ソリューション エクスプローラーで、データセット ファイルを選択します。

    2. ファイルを右クリックし、ドロップダウン メニューの [削除] を選択します。

使用例

基本的な Visual Studio アドインの例を次に示します。この例には、Visual Studio オートメーションを使用して BuildManager オブジェクトと BuildManagerEvents オブジェクトを使用する方法が示されています。

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 のオートメーションおよび機能拡張