Share via


방법: 특정 프로젝트의 이벤트에 응답(Visual C#)

업데이트: 2007년 11월

자동화 모델에는 Visual Studio IDE(통합 개발 환경)의 환경 이벤트에 대한 응답으로 사용할 수 있는 개체가 포함되어 있습니다. VSLangProjVSLangProj80에 정의되어 있는 환경 이벤트는 Visual C#, Visual Basic 및 Visual J# 프로젝트에 적용됩니다. 예를 들어, Visual Basic 프로젝트에서 가져오기를 추가 또는 제거하면 ImportsEvents가 발생합니다.

이 예제에서는 프로젝트 형식에 적용되는 ReferencesEvents 이벤트 처리기를 Visual C#을 사용하여 추가 기능 프로젝트에 추가합니다. ReferencesEvents는 Visual C#, Visual Basic 또는 Visual J# 프로젝트에서 참조를 변경, 추가 또는 제거하는 경우에 발생합니다.

참고:

표시되는 대화 상자와 메뉴 명령은 실제 설정이나 버전에 따라 도움말에서 설명하는 것과 다를 수 있습니다. 이러한 절차는 일반 개발 설정을 사용하여 개발되었습니다. 설정을 변경하려면 도구 메뉴에서 설정 가져오기 및 내보내기를 선택합니다. 자세한 내용은 Visual Studio 설정을 참조하십시오.

Visual C#을 사용하여 참조 관련 이벤트를 처리하려면

  1. Visual C#에서 Visual Studio 추가 기능 프로젝트를 만듭니다.

  2. Connect.cs 파일의 맨 위에 using VSLangProj;를 추가합니다.

  3. 프로젝트 메뉴에서 참조 추가를 클릭하고 .NET 탭을 클릭한 다음 첫 번째 VSLangProj를 선택하고 확인을 클릭합니다.

  4. Connect 클래스에서 ReferencesEvents 개체를 처리하기 위한 변수와 OutputWindowPane을 처리하기 위한 변수를 각각 초기화합니다.

        private DTE2 _applicationObject;
        private AddIn _addInInstance;
        private VSLangProj.ReferencesEvents refEvents;private OutputWindowPane outputWinPane;
    

    이 예제에서 변수 이름은 refEvents입니다.

    자동화 모델의 다른 개체는 프로젝트에 적용되는 다른 이벤트 형식과 연결됩니다. 예를 들어, Imports 컬렉션에서 가져오기를 추가 또는 제거하면 ImportsEvents가 발생합니다. BuildManagerEvents는 사용자 지정 도구의 출력을 사용하여 빌드한 임시 어셈블리와 관련된 이벤트에 적용됩니다. BuildManager 개체에 대한 자세한 내용은 BuildManager 개체 소개를 참조하십시오. 프로젝트 형식과 관련된 이벤트의 전체 목록은 이벤트 개체(프로젝트 형식별)를 참조하십시오. 일반 자동화 이벤트의 목록은 자동화 이벤트 개체를 참조하십시오.

  5. OnConnection 메서드에서 이벤트를 가로채는 변수를 초기화합니다. 이 예제에서 변수는 events입니다.

    EnvDTE80.Events2 events = 
    (EnvDTE80.Events2)_applicationObject.Events;
    
  6. OnConnection 메서드에서 OutputWindow 변수를 초기화합니다.

    OutputWindow outputWindow = (OutputWindow)_applicationObject.Windows.Item
    (Constants.vsWindowKindOutput).Object;
    outputWinPane = outputWindow.OutputWindowPanes.Add
    ("ReferencesEvents Event Information");
    
  7. 또한 OnConnection 메서드에서 자동화 모델의 이벤트 개체를 검색합니다.

    refEvents = 
    (VSLangProj.ReferencesEvents)events.GetObject
    ("CSharpReferencesEvents");
    

    이 예제에서 ReferencesEvents는 Visual C# 프로젝트에 적용됩니다. Visual Basic 또는 Visual J# 관련 이벤트에 응답하려면 문자열 CSharpReferencesEvents를 각각 VBReferencesEvents 또는 VJSharpReferencesEvents로 바꿉니다. 각기 다른 형식의 프로젝트에 해당하는 이벤트에 사용할 문자열을 결정하는 방법에 대한 자세한 내용은 이벤트 개체(프로젝트 형식별)를 참조하십시오.

  8. 3단계에서 검색한 이벤트 개체가 노출하는 각 대리자를 += 연산자를 사용하여 연결합니다. 예를 들어, ReferenceAdded 이벤트가 노출하는 대리자를 연결하려면 다음 코드를 사용합니다.

    refEvents.ReferenceAdded += new 
    _dispReferencesEvents_ReferenceAddedEventHandler
    (this.ReferenceAdded);
    
  9. 이벤트 개체와 관련된 각 이벤트에 대한 프로시저를 추가합니다. 예를 들어, 참조를 추가할 때 발생하는 이벤트를 처리하려면 다음 코드를 사용합니다.

    public void ReferenceAdded( VSLangProj.Reference addedRef ) 
    { 
        outputWinPane.OutputString( "ReferencesEvents.ReferenceAdded" 
    + "\n" ); 
        outputWinPane.OutputString( "The reference to " + addedRef.Name
     + " was added." + "\n" ); 
    }
    

    ReferencesEvents의 경우 다음에 대해 정의된 이벤트가 있어야 합니다.

    이러한 이벤트는 아래 예제의 전체 목록에 포함되어 있습니다.

  10. 마지막으로, 추가 기능을 닫은 후에도 Visual Studio에서 창 관련 이벤트를 계속 모니터링하면 시스템 속도가 느려질 수 있으므로 이벤트 처리를 사용하지 않도록 설정합니다. Visual C#의 경우 -= 연산자를 사용합니다. 예를 들어, ReferenceAdded에 대한 이벤트 처리를 비활성화하려면 다음 코드를 사용합니다.

    refEvents.ReferenceAdded -= new
     _dispReferencesEvents_ReferenceAddedEventHandler
    (this.ReferenceAdded);
    

    이렇게 하면 추가 기능이 종료될 경우만이 아니라 추가 기능이 여전히 실행 중인 상태에서 IDE가 종료될 경우에도 이벤트 처리가 해제됩니다. IDE가 종료될 때에는 실행 중인 모든 추가 기능이 먼저 자동으로 종료됩니다.

예제

다음은 Visual Studio에서 Visual C# 참조 이벤트를 가로채고 처리하는 방법을 보여 주는 기본 Visual Studio 추가 기능 예제입니다. 참조 이벤트가 발생할 때마다 알림 메시지가 출력 창으로 전달됩니다.

using System;
using Microsoft.VisualStudio.CommandBars;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using VSLangProj;

public Connect()
  {
  }
  public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
  {
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    // Retrieve the event objects from the automation model.
    EnvDTE80.Events2 events = 
(EnvDTE80.Events2)_applicationObject.Events;
    // Send event messages to the Output window.
    OutputWindow outputWindow =
 (OutputWindow)_applicationObject.Windows.Item
(Constants.vsWindowKindOutput).Object;
    outputWinPane = 
outputWindow.OutputWindowPanes.Add
("ReferencesEvents Event Information");

    // Retrieve the event objects from the automation model.
    refEvents = (VSLangProj.ReferencesEvents)
events.GetObject("CSharpReferencesEvents");

    // Connect to each delegate exposed from each object 
    // retrieved above.
    refEvents.ReferenceAdded += new
 _dispReferencesEvents_ReferenceAddedEventHandler
(this.ReferenceAdded);
    refEvents.ReferenceChanged += new
 _dispReferencesEvents_ReferenceChangedEventHandler
(this.ReferenceChanged);
    refEvents.ReferenceRemoved += new
 _dispReferencesEvents_ReferenceRemovedEventHandler
(this.ReferenceRemoved); 
  }

  public void OnDisconnection(Extensibility.ext_DisconnectMode
 disconnectMode, ref System.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 they have not been garbage collected.
    if (refEvents != null)
    {
        refEvents.ReferenceAdded -= new
 _dispReferencesEvents_ReferenceAddedEventHandler
(this.ReferenceAdded);
        refEvents.ReferenceChanged -= new
 _dispReferencesEvents_ReferenceChangedEventHandler
(this.ReferenceChanged);
        refEvents.ReferenceRemoved -= new
 _dispReferencesEvents_ReferenceRemovedEventHandler
(this.ReferenceRemoved); 
    }
  }

  // References related events.
  public void ReferenceRemoved( VSLangProj.Reference removedRef ) 
  { 
    outputWinPane.OutputString( "ReferencesEvents.ReferenceRemoved"
 + "\n" ); 
    outputWinPane.OutputString( "The reference to " + removedRef.Name
 + " was removed." + "\n" ); 
  } 

  public void ReferenceChanged( VSLangProj.Reference changedRef ) 
  { 
    outputWinPane.OutputString( "ReferencesEvents.ReferenceChanged" 
+ "\n" ); 
    outputWinPane.OutputString( "The reference to " + changedRef.Name 
+ " was changed." + "\n" ); 
  } 
        
  public void ReferenceAdded( VSLangProj.Reference addedRef ) 
  { 
    outputWinPane.OutputString( "ReferencesEvents.ReferenceAdded" +
 "\n" ); 
    outputWinPane.OutputString( "The reference to " + addedRef.Name 
+ " was added." + "\n" ); 
  } 
  public void OnAddInsUpdate(ref System.Array custom)
  {
  }
  public void OnStartupComplete(ref System.Array custom)
  {
  }
  public void OnBeginShutdown(ref System.Array custom)
  {
  }
  private DTE2 _applicationObject;
  private AddIn _addInInstance;
  private VSLangProj.ReferencesEvents refEvents;
  private OutputWindowPane outputWinPane;
  }}

코드 컴파일

이 코드를 컴파일하려면 Visual C#에서 새 Visual Studio 추가 기능 프로젝트를 만들고 Connect 클래스의 코드를 예제에 있는 코드로 변경합니다. 추가 기능을 실행하는 방법에 대한 내용은 방법: 추가 기능 관리자를 사용하여 추가 기능 제어를 참조하십시오.

참고 항목

작업

방법: 특정 프로젝트의 이벤트에 응답(Visual Basic)

참조

+= 연산자(C# 참조)

-= 연산자(C# 참조)

기타 리소스

자동화 이벤트에 응답

이벤트에 응답(Visual Basic 및 Visual C# 프로젝트)