다음을 통해 공유


How to: Manipulate Code by Using the Visual C++ Code Model (Visual C#)

In Visual Studio 2013, add-ins are deprecated. We recommend that you upgrade your add-ins to VSPackage extensions. For more information about how to upgrade, see FAQ: Converting Add-ins to VSPackage Extensions.

The Visual Studio code model offers automation clients the ability to find code definitions in a project and modify those code elements. Visual C++ provides an extension to the core code model to target code that is specific to Visual C++.

For example, if the Language property indicates that a given code element is a Visual C++ code model object, and Kind = vsCMElementClass, then you can choose to use either the CodeClass2 from the Visual Studio code model or the VCCodeClass from the Visual C++ code model.

The following procedures demonstrate how to examine and generate code by using the code model that is specific to Visual C++.

To add a comment to the first file in the project

  1. Create a Visual Studio add-in project in Visual C#.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.

  3. Add using Microsoft.VisualStudio.VCCodeModel; to the top of the Connect.cs file.

  4. Replace the code in the OnConnection method with the following code:

    // 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. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio integrated development environment (IDE).

  7. On the Tools menu, click Add-in Manager, and then select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.

  8. Examine the first file in the project for the programmatically added comments.

To add a new file to a Visual C++ project

  1. Create a Visual Studio add-in project in Visual C#.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.

  3. Add using Microsoft.VisualStudio.VCCodeModel; to the top of the Connect.cs file.

  4. Replace the code in the OnConnection method with the following code:

    //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. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio IDE.

  7. On the Tools menu, click Add-in Manager, and select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.

    Note

    If MyClass2.h already exists, the code fails.

To add a function to file.h

  1. Create a Visual Studio add-in project in Visual C#.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel and System.Windows.Forms, and then click OK.

  3. Add the following using statements to the top of the Connect.cs file:

    using System.Windows.Forms;
    using Microsoft.VisualStudio.VCCodeModel;
    
  4. Replace the code in the OnConnection method with the following code:

    // 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. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio IDE and add a file.h to it.

  7. On the Tools menu, click Add-in Manager, and then select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.

  8. Examine the inserted code in file.h.

To display files that include top-level code elements

  1. Create a Visual Studio add-in project in Visual C#.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel and System.Windows.Forms, and then click OK.

  3. Add the following using statements to the top of the Connect.cs file:

    using System.Windows.Forms;
    using Microsoft.VisualStudio.VCCodeModel;
    
  4. Replace the code in the OnConnection method with the following code:

    // 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. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio IDE.

  7. On the Tools menu, click Add-in Manager, and then select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.

  8. Message boxes display the file names that contain the top-level code elements.

To display all the top level code element items

  1. Create a Visual Studio add-in project in Visual C#.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel and System.Windows.Forms, and then click OK.

  3. Add the following using statements to the top of the Connect.cs file:

    using System.Windows.Forms;
    using Microsoft.VisualStudio.VCCodeModel;
    
  4. Replace the code in the OnConnection method with the following code:

    // 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. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio IDE.

  7. On the Tools menu, click Add-in Manager, and then select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.

    Message boxes display the top level code element names.

See Also

Tasks

How to: Manipulate Code by Using the Visual C++ Code Model (Visual Basic)

Concepts

Visual C++ Code Model

Discovering Code by Using the Code Model (Visual Basic)

Discovering Code by Using the Code Model (Visual C#)