How to get services from the VS DTE (using System.IServiceProvider)

Note: this entry has moved.

VS is heavily based on a component model and container hierarchy of services (yes, that's most probably where System.ComponentModel came from...). Even though there's no comprehensive documentation of all services available and from which contexts, every now and then I find the need to ask for services, but all I've got at hand is either a EnvDTE.ProjectItem or Project, or just the DTE. So, how do you use the familiar System.IServiceProvider to ask for services?

The trick is to get the latest VS SDK, add a reference to Microsoft.VisualStudio.OLE.Interop.dll and Microsoft.VisualStudio.Shell.dll and use the following simple code:

EnvDTE.Project project; // this is what you have at hand somehow.
IServiceProvider serviceProvider = new ServiceProvider(project.DTE as
DynamicTypeService typeService = (DynamicTypeService)

The ServiceProvider class in the Shell assembly provides the adapter you need to ask the DTE for services.

No Comments