Walkthrough: Access the DTE object from an editor extension

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.

Get the DTE object

  1. Create a C# VSIX project and name it DTETest. Add an Editor Classifier item template and name it DTETest.

    For more information, see Create an extension with an editor item template.

  1. Add the following assembly reference to the project:

    • Microsoft.VisualStudio.Shell.Immutable.10.0
  2. In the DTETestProvider.cs file, add the following using directives:

    using EnvDTE;
    using Microsoft.VisualStudio.Shell;
    
  3. In the DTETestProvider class, import an SVsServiceProvider.

    [Import]
    internal SVsServiceProvider ServiceProvider = null;
    
  4. In the GetClassifier() method, add the following code before the return statement:

    ThreadHelper.ThrowIfNotOnUIThread();
    DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));
    
  1. Add the following assembly references to the project:

    • EnvDTE
    • Microsoft.VisualStudio.Shell.Framework
  2. In the DTETestProvider.cs file, add the following using directives:

    using EnvDTE;
    using Microsoft.VisualStudio.Shell;
    
  3. In the DTETestProvider class, import an SVsServiceProvider.

    [Import]
    internal SVsServiceProvider ServiceProvider = null;
    
  4. In the GetClassifier() method, add the following code before the return statement:

    DTE dte = (DTE)ServiceProvider.GetService(typeof(DTE));
    

See also