Partilhar via


ServiceProvider.GetService Method

Definition

Gets GUID-based services from the unmanaged service provider.

Overloads

GetService(Guid)

Gets the specified service from the unmanaged service provider.

GetService(Type)

Gets type-based services from the unmanaged service provider.

GetService(Type, Boolean)

Retrieves the requested service.

Remarks

Services are identified for retrieval by an associated GUID. ServiceProvider allows services to be requested directly with a GUID, but is overloaded to permit requests by service type as well.

GetService(Guid)

Gets the specified service from the unmanaged service provider.

public:
 System::Object ^ GetService(Guid guid);
public object GetService (Guid guid);
member this.GetService : Guid -> obj
Public Function GetService (guid As Guid) As Object

Parameters

guid
Guid

The GUID of the service to retrieve.

Returns

The requested service, or null if the service could not be located.

Applies to

GetService(Type)

Gets type-based services from the unmanaged service provider.

public:
 virtual System::Object ^ GetService(Type ^ serviceType);
public:
 Platform::Object ^ GetService(Platform::Type ^ serviceType);
public object GetService (Type serviceType);
abstract member GetService : Type -> obj
override this.GetService : Type -> obj
Public Function GetService (serviceType As Type) As Object

Parameters

serviceType
Type

The type of service to retrieve. The GUID of this type is used to obtain the service from the native service provider.

Returns

The requested service, or null if the service could not be located.

Implements

Remarks

Managed VSPackages can use GetService to get VSSDK COM interfaces by querying the interop assemblies. For more information, see Interop Namespaces.

To get a specific VSSDK interface:

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

  • The return value of GetService must be cast to the interface type. The casting is necessary because GetService queries the input type for the GUID of the service that provides it. An IUnknown interface on the class implementing the service is then returned, and must be cast to obtain the desired object. (The object must be supported on the underlying service.)

    For example, one could get an IVsUIShell interface with:

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

Note

For historical reasons, the IVsTextManager interface that 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

GetService(Type, Boolean)

Retrieves the requested service.

public:
 System::Object ^ GetService(Type ^ serviceType, bool setShellErrorInfo);
public object GetService (Type serviceType, bool setShellErrorInfo);
member this.GetService : Type * bool -> obj
Public Function GetService (serviceType As Type, setShellErrorInfo As Boolean) As Object

Parameters

serviceType
Type

The ID of the service to acquire.

setShellErrorInfo
Boolean

true to save any exception information such that it isn't lost when converted to an HRESULT. Must be false when called off the main thread.

Returns

Applies to