共用方式為


HOW TO:使用 Visual C++ 程式碼模型管理程式碼 (Visual C#)

Visual Studio 程式碼模型會提供 Automation 用戶端在專案中尋找程式碼定義及修改那些程式碼項目的能力。 Visual C++ 則提供副檔名給核心程式碼模型,指定以 Visual C++ 專用的程式碼做為目標。

例如,如果 Language 屬性表示指定的程式碼項目是 Visual C++ 程式碼模型物件,且 Kind = vsCMElementClass,則您可以選擇使用 Visual Studio 程式碼模型的 CodeClass2 或 Visual C++ 程式碼模型的 VCCodeClass

下列程序會示範如何使用 Visual C++ 專用的程式碼模型來檢查並產生程式碼。

若要將註解加入至專案中的第一個檔案

  1. 在 Visual C# 中建立 Visual Studio 增益集專案。

  2. 在 [專案] 功能表上按一下 [加入參考],再按一下 [.NET] 索引標籤,然後選取 [Microsoft.VisualStudio.VCCodeModel],之後按一下 [確定]。

  3. 將 using Microsoft.VisualStudio.VCCodeModel; 加入至 Connect.cs 檔案的最上方。

  4. 用下列程式碼取代 OnConnection 方法中的程式碼:

    // Add-in code.
    using Microsoft.VisualStudio.VCCodeModel;
    public void OnConnection(object application,
     Extensibility.ext_ConnectMode connectMode, object addInInst,
     ref System.Array custom)
    {
        _applicationObject = (DTE2)application;
        )addInInstance = (AddIn)addInInst;
        // Pass the applicationObject member variable to the code example.
        test((DTE2)_applicationObject); 
    }
    
    public void test( DTE2 dte ) 
    { 
        VCCodeModel vcCM = null; 
        VCCodeElement vcCodeElement = null; 
        vcCM = ( ( VCCodeModel )( dte.Solution.Item( 1 ).CodeModel ) ); 
        vcCodeElement = ( ( VCCodeElement )
    ( vcCM.CodeElements.Item(1))); 
        AddCommentAtStart( vcCodeElement ); 
        AddCommentAtEnd( vcCodeElement ); 
    } 
    
    public void AddCommentAtStart(
      Microsoft.VisualStudio.VCCodeModel.VCCodeElement vcCodeElement )
    {
        TextPoint textPoint = null;
        textPoint = vcCodeElement.get_StartPointOf(
          vsCMPart.vsCMPartWhole, 0 );
        textPoint.CreateEditPoint().Insert("/*This is a Start Comment*/");
    }
    
    public void AddCommentAtEnd( 
      Microsoft.VisualStudio.VCCodeModel.VCCodeElement vcCodeElement )
    {
        TextPoint textPoint = null;
        textPoint = vcCodeElement.get_EndPointOf( vsCMPart.vsCMPartWhole, 0  
          );
        textPoint.CreateEditPoint().Insert( "/*End Comment*/" );
    }
    
  5. 若要建置增益集,請按一下 [建置] 功能表上的 [建置方案]。

  6. 在 Visual Studio 整合式開發環境 (IDE) 中開啟 Visual C++ 專案。

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

  8. 在專案的第一個檔案中檢查以程式設計方式加入的註解。

若要將新檔案加入至 Visual C++ 專案

  1. 在 Visual C# 中建立 Visual Studio 增益集專案。

  2. 在 [專案] 功能表上按一下 [加入參考],再按一下 [.NET] 索引標籤,然後選取 [Microsoft.VisualStudio.VCCodeModel],之後按一下 [確定]。

  3. 將 using Microsoft.VisualStudio.VCCodeModel; 加入至 Connect.cs 檔案的最上方。

  4. 用下列程式碼取代 OnConnection 方法中的程式碼:

    //Add-in code.
    using Microsoft.VisualStudio.VCCodeModel;
    public void OnConnection(object application,
     Extensibility.ext_ConnectMode connectMode, object addInInst,
     ref System.Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        // Pass the applicationObject member variable to the code example.
        GetVCCodeElement((DTE2)_applicationObject); 
    }
    
    //  Shows how to get a VCCodeElement.
    public void GetVCCodeElement( DTE2 dte ) 
    {
        VCCodeModel vcCM = null; 
        VCCodeElement vcCodeElement = null; 
        vcCM = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeModel )(
          dte.Solution.Item( 1 ).CodeModel ) ); 
        vcCodeElement = ( (
          Microsoft.VisualStudio.VCCodeModel.VCCodeElement )( 
          vcCM.AddClass( "MyClass2", "MyClass2.h",0,null, null,
          EnvDTE.vsCMAccess.vsCMAccessDefault ) ) ); 
    }
    
  5. 若要建置增益集,請按一下 [建置] 功能表上的 [建置方案]。

  6. 在 Visual Studio IDE 中,開啟 Visual C++ 專案。

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

    注意事項注意事項

    如果 MyClass2.h 已經存在,則程式碼會失敗。

若要將功能加入至 file.h

  1. 在 Visual C# 中建立 Visual Studio 增益集專案。

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

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

    using System.Windows.Forms;
    using Microsoft.VisualStudio.VCCodeModel;
    
  4. 用下列程式碼取代 OnConnection 方法中的程式碼:

    // Add-in code.
    using Microsoft.VisualStudio.VCCodeModel;
    using System.Windows.Forms;
    public void OnConnection(object application,
     Extensibility.ext_ConnectMode connectMode, object addInInst,
     ref System.Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        // Pass the applicationObject member variable to the code example.
        DisplayName((DTE2)_applicationObject); 
    }
    
    // DisplayName
    // Shows the DisplayName of a function which includes the parameter 
    // names.
    public void DisplayName( DTE2 dte ) 
    { 
        VCCodeModel vcCM = null; 
        VCCodeElement vcCodeElement = null; 
        vcCM = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeModel )(
          dte.Solution.Item( 1 ).CodeModel ) ); 
          vcCodeElement = ( (     
            Microsoft.VisualStudio.VCCodeModel.VCCodeElement )
            ( vcCM.AddFunction( "MyFunction", "File.h",  
            vsCMFunction.vsCMFunctionFunction, "void",
            null, EnvDTE.vsCMAccess.vsCMAccessDefault ) ) ); 
        MessageBox.Show( vcCodeElement.DisplayName); 
    }
    
  5. 若要建置增益集,請按一下 [建置] 功能表上的 [建置方案]。

  6. 開啟 Visual Studio IDE 中的 Visual C++ 專案,然後將 file.h 加入其中。

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

  8. 檢查 file.h 中的插入程式碼。

若要顯示包含最上層程式碼項目的檔案

  1. 在 Visual C# 中建立 Visual Studio 增益集專案。

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

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

    using System.Windows.Forms;
    using Microsoft.VisualStudio.VCCodeModel;
    
  4. 將 OnConnection 方法中的程式碼取代為下列程式碼:

    // Add-in code.
    using Microsoft.VisualStudio.VCCodeModel;
    using System.Windows.Forms;
    public void OnConnection(object application,
     Extensibility.ext_ConnectMode connectMode, object addInInst,
     ref System.Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        // Pass the applicationObject member variable to the code example.
        DisplayLocation((DTE2)_applicationObject); 
    }
    
    public void DisplayLocation( DTE2 dte ) 
    {
        VCCodeModel vcCM = null; 
        VCCodeElement vcCodeElement = null; 
        vcCM = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeModel )(
          dte.Solution.Item( 1 ).CodeModel ) ); 
        foreach ( Microsoft.VisualStudio.VCCodeModel.VCCodeElement temp
          in vcCM.CodeElements ) 
        {
            vcCodeElement = temp;
            MessageBox.Show( vcCodeElement.Name + " is declared in " 
              + vcCodeElement.get_Location(vsCMWhere.vsCMWhereDefault)); 
        }
    }
    
  5. 若要建置增益集,請按一下 [建置] 功能表上的 [建置方案]。

  6. 在 Visual Studio IDE 中,開啟 Visual C++ 專案。

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

  8. 訊息方塊會顯示包含最上層程式碼項目的檔案名稱。

若要顯示所有最上層程式碼項目

  1. 在 Visual C# 中建立 Visual Studio 增益集專案。

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

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

    using System.Windows.Forms;
    using Microsoft.VisualStudio.VCCodeModel;
    
  4. 將 OnConnection 方法中的程式碼取代為下列程式碼:

    // Add-in code.
    using Microsoft.VisualStudio.VCCodeModel;
    using System.Windows.Forms;
    public void OnConnection(object application,
     Extensibility.ext_ConnectMode connectMode, object addInInst,
     ref System.Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        // Pass the applicationObject member variable to the code example.
        FindItem((DTE2)_applicationObject); 
    }
    
    public void FindItem( DTE2 dte ) 
    {
        VCCodeModel vcCM = null; 
        VCCodeElements vcCodeElements = null; 
        vcCM = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeModel )(
          dte.Solution.Item( 1 ).CodeModel ) ); 
        vcCodeElements =
          ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeElements )
          ( vcCM.CodeElements ) ); 
        int i = 0; 
        for ( i=1; i<=vcCodeElements.Count; i++ ) 
        {
            MessageBox.Show( vcCodeElements.Item( i ).Name); 
        }
    }
    
  5. 若要建置增益集,請按一下 [建置] 功能表上的 [建置方案]。

  6. 在 Visual Studio IDE 中,開啟 Visual C++ 專案。

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

    訊息方塊會顯示最上層程式碼項目名稱。

請參閱

工作

HOW TO:使用 Visual C++ 程式碼模型管理程式碼 (Visual Basic)

概念

Visual C++ 程式碼模型

使用程式碼模型探索程式碼 (Visual Basic)

使用程式碼模型探索程式碼 (Visual C#)