Share via


Walkthrough: Accessing the DTE Object from an Editor Extension

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

In VSPackages, you can get the DTE object by calling the GetService method with the type of the DTE object. In Managed Extensibility Framework (MEF) extensions, you can import SVsServiceProvider and then call the GetService method with a type of DTE.

Prerequisites

To follow this walkthrough, you must install the Visual Studio SDK. For more information, see Visual Studio SDK.

Getting the DTE Object

To get the DTE object from the ServiceProvider

  1. Create a C# VSIX project named DTETest. Add an Editor Classifier item template and name it DTETest. For more information, see Creating an Extension with an Editor Item Template.

  2. Add the following assembly references to the project:

    • EnvDTE

    • EnvDTE80

    • Microsoft.VisualStudio.Shell.Immutable.10.0

  3. Go to the DTETest.cs file, and add the following using directives:

    using EnvDTE;  
    using EnvDTE80;  
    using Microsoft.VisualStudio.Shell;  
    
    
  4. In the GetDTEProvider class, import a SVsServiceProvider.

    [Import]  
    internal SVsServiceProvider ServiceProvider = null;  
    
    
  5. In the GetClassifier() method, add the following code.

    DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));  
    
    
  6. If you have to use the DTE2 interface, you can cast the DTE object to it.

See Also

Language Service and Editor Extension Points