Share via


方法 : Visual C++ コード モデルを使用したコード操作 (Visual C#)

Visual Studio 2013 では、アドインは推奨されていません。 アドインを VSPackage 拡張機能にアップグレードすることをお勧めします。 アップグレード方法の詳細については、「FAQ: アドインを VSPackage 拡張に変換する」を参照してください。

Visual Studio コード モデルでは、オートメーション クライアントから、プロジェクト内のコード定義を検索したり、見つかったコード要素に変更を加えたりできます。 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 を選択し、[OK] をクリックします。

  3. Connect.cs ファイルの先頭に using Microsoft.VisualStudio.VCCodeModel; を追加します。

  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: Integrated Development Environment) で、Visual C++ プロジェクトを開きます。

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

  8. プロジェクトの 1 つ目のファイルで、プログラムによって追加されたコメントをチェックします。

Visual C++ プロジェクトに新しいファイルを追加するには

  1. Visual C# で Visual Studio のアドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックします。次に、[.NET] タブをクリックして Microsoft.VisualStudio.VCCodeModel を選択し、[OK] をクリックします。

  3. Connect.cs ファイルの先頭に using Microsoft.VisualStudio.VCCodeModel; を追加します。

  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. [ツール] メニューの [アドイン マネージャー] をクリックし、[アドイン マネージャー] ダイアログ ボックスからアドインを選択します。 [OK] をクリックしてアドインを実行します。

    注意

    MyClass2.h が既に存在する場合はエラーになります。

関数を file.h に追加するには

  1. Visual C# で Visual Studio のアドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックします。次に、[.NET] タブをクリックし、Microsoft.VisualStudio.VCCodeModel と System.Windows.Forms を選択して、[OK] をクリックします。

  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. [ツール] メニューの [アドイン マネージャー] をクリックし、[アドイン マネージャー] ダイアログ ボックスからアドインを選択します。 [OK] をクリックしてアドインを実行します。

  8. file.h で挿入されたコードをチェックします。

最上位のコード要素を含むファイルを表示するには

  1. Visual C# で Visual Studio のアドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックします。次に、[.NET] タブをクリックし、Microsoft.VisualStudio.VCCodeModel と System.Windows.Forms を選択して、[OK] をクリックします。

  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. [ツール] メニューの [アドイン マネージャー] をクリックし、[アドイン マネージャー] ダイアログ ボックスからアドインを選択します。 [OK] をクリックしてアドインを実行します。

  8. 最上位のコード要素を含むファイル名がメッセージ ボックスに表示されます。

最上位のコード要素項目をすべて表示するには

  1. Visual C# で Visual Studio のアドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックします。次に、[.NET] タブをクリックし、Microsoft.VisualStudio.VCCodeModel と System.Windows.Forms を選択して、[OK] をクリックします。

  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. [ツール] メニューの [アドイン マネージャー] をクリックし、[アドイン マネージャー] ダイアログ ボックスからアドインを選択します。 [OK] をクリックしてアドインを実行します。

    最上位のコード要素名がメッセージ ボックスに表示されます。

参照

処理手順

方法 : Visual C++ コード モデルを使用したコード操作 (Visual Basic)

概念

Visual C++ コード モデル

コード モデルを使用したコードの調査 (Visual Basic)

コード モデルを使用したコードの調査 (Visual C#)