Visual Studio API Reference

Visual Studio Extensibility APIs

This section provides documentation for APIs that allow you to extend and customize the Visual Studio Integrated Development Environment (IDE).

EnvDTE & EnvDTE80

The EnvDTE and EnvDTE80 namespaces provide programmatic access to the Visual Studio IDE's objects, methods, and properties. You can use these to automate tasks, create custom tools, and interact with the IDE's features.

Example: Getting the active document

Accessing the active document in the code editor:


// C# Example
using EnvDTE;

DTE dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(DTE));
if (dte != null)
{
    Document activeDoc = dte.ActiveDocument;
    if (activeDoc != null)
    {
        Console.WriteLine("Active document: " + activeDoc.Name);
    }
}
                

VSCommandBar API

The VSCommandBar API allows you to create, manipulate, and manage command bars (menus, toolbars) within Visual Studio. This is crucial for adding custom commands and UI elements to the IDE.

Key Concepts:

  • CommandBar: Represents a command bar (e.g., File menu, Standard toolbar).
  • CommandBarControl: Represents an item on a command bar (e.g., menu item, button).
  • CommandBarButton: A specific type of control for buttons.

Package Extensions

Visual Studio extensions are typically implemented as managed packages. These packages can leverage the DTE objects and command bar APIs to integrate their functionality.

Common Tasks:

  • Adding new menus and submenus.
  • Adding buttons to existing toolbars.
  • Responding to user clicks on custom commands.
  • Opening custom editor windows or dialogs.

Extensibility Model (MEF)

The Managed Extensibility Framework (MEF) is a core component for building modern Visual Studio extensions. It provides a composition model for discovering and loading extension components.

Key MEF Concepts:

  • Parts: Components that export or import functionality.
  • Exports: Functionality offered by a part.
  • Imports: Functionality required by a part.
  • Composition: The process of connecting imports to exports.

Example: Exporting a Tool Window


// C# Example using MEF
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.ComponentModel.Composition;

[Export(typeof(EditorPane))]
[PartMetadata(ExportContractNames.VsPackage.VsPackageToolWindowService, ExportContractNames.VsPackage.VsPackageToolWindowService)]
public class MyToolWindow : ToolWindowPane
{
    // ... Tool window implementation ...
}
                

Debugging APIs

Visual Studio offers APIs for interacting with the debugging engine, allowing you to build custom debuggers or integrate with existing ones.