방법: BuildManager 및 BuildManagerEvents 개체 사용

BuildManager 개체는 디자인 타임 출력을 생성하는 사용자 지정 도구(단일 파일 생성기)를 실행하여 생성된 PE(이식 가능한 실행) 파일을 관리하고 표시하는 데 사용됩니다. BuildManagerEvents 이벤트는 임시 PE를 생성하는 프로젝트 항목이 변경되거나 삭제될 때 발생합니다.

이 문서에서는 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. Connect.cs 파일의 맨 위에 다음과 같은 using 문을 추가합니다.

    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 파일에 대한 모니커를 표시하는 코드를 추가합니다.

    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. 프로젝트에 데이터 집합을 추가하려면 프로젝트 메뉴에서 새 항목 추가를 클릭합니다. 새 항목 추가 대화 상자에서 데이터 집합을 선택하고 확인을 클릭합니다.

    DataSet 파일은 프로젝트와 관련된 사용자 지정 도구(단일 파일 생성기)를 확보하는 데 사용됩니다.

도구 메뉴에서 추가 기능 관리자를 클릭하고 추가 기능 관리자 대화 상자에서 추가 기능을 선택합니다. 확인을 클릭하여 추가 기능을 실행합니다.

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의 자동화 및 확장성