Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

Package.GetService(Type) Method

Definition

Namespace:
Microsoft.VisualStudio.Shell
Assembly:
Microsoft.VisualStudio.Shell.14.0.dll
Assembly:
Microsoft.VisualStudio.Shell.15.0.dll
Package:
Microsoft.VisualStudio.Shell.15.0 v17.14.40264

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Gets type-based services from the VSPackage service container.

protected:
 virtual System::Object ^ GetService(Type ^ serviceType);
protected:
 virtual Platform::Object ^ GetService(Platform::Type ^ serviceType);
protected virtual object GetService(Type serviceType);
protected virtual object? GetService(Type serviceType);
abstract member GetService : Type -> obj
override this.GetService : Type -> obj
Protected Overridable Function GetService (serviceType As Type) As Object

Parameters

serviceType
Type

The type of service to retrieve.

Returns

An instance of the requested service, or null if the service could not be found.

Exceptions

serviceType is null.

Remarks

Managed VSPackages can use GetService to get Environment SDK COM interfaces by querying the interop assemblies of the SDK.

To get a specific Environment SDK interface:

  1. GetService should be called with a serviceType returned by using that interface as an argument to typeof.

  2. The return value of GetService must be cast to the interface type.

The casting is necessary because GetService searches for on the basis service type GUID with an interface type of IUnknown.

For example, one could get an IVsUIShell interface with:

myUIShell = myPackage.GetService(System.typeof(IVsUIShell)) as IVsUIShell;

Note

For historical reasons, the IVsTextManager interface cannot be obtained in this manner. To obtain an IVsTextManager interface, first use VsTextManagerClass (the class implementing the interface) as the argument to typeof, then cast the return value of GetService to IVsTextManager, for instance: IVsTextManager mytext_mgr = myPackage.GetService(System.typeof(VsTextManagerClass)) as IVsTextManager;

Applies to