Obtaining a ServiceProvider from a non sited component
Many times we need to obtain a VS Service inside a VS Package from a component that is not sited.
We can create a ServiceProvider by using a DTE instance:
IServiceProvider serviceProvider =
newServiceProvider(dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
But what if we dont have access to a DTE instance at all?
We can solve this issue by using the static GetGlobalService method of the Package class:
EnvDTE.DTE dte = Package.GetGlobalService(typeof(EnvDTE.DTE));
IServiceProvider serviceProvider =
newServiceProvider(dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
IVsSolution solution = serviceProvider.GetType(SVsSolution) asIVsSolution;
Note:
We will need to add a reference to Microsoft.VisualStudio.OLE.Interop.dll and Microsoft.VisualStudio.Shell.dll
Pablo