Condividi tramite


ServiceProvider.GetService Method (Type)

Gets type-based services from the unmanaged service provider.

Namespace:  Microsoft.VisualStudio.Shell
Assemblies:   Microsoft.VisualStudio.Shell (in Microsoft.VisualStudio.Shell.dll)
  Microsoft.VisualStudio.Shell.10.0 (in Microsoft.VisualStudio.Shell.10.0.dll)
  Microsoft.VisualStudio.Shell.9.0 (in Microsoft.VisualStudio.Shell.9.0.dll)

Syntax

'Dichiarazione
Public Function GetService ( _
    serviceType As Type _
) As Object
'Utilizzo
Dim instance As ServiceProvider
Dim serviceType As Type
Dim returnValue As Object

returnValue = instance.GetService(serviceType)
public Object GetService(
    Type serviceType
)
public:
virtual Object^ GetService(
    Type^ serviceType
) sealed
abstract GetService : 
        serviceType:Type -> Object 
override GetService : 
        serviceType:Type -> Object 
public final function GetService(
    serviceType : Type
) : Object

Parameters

  • serviceType
    Type: System.Type
    The type of service to retrieve. The GUID of this type is used to obtain the service from the native service provider.

Return Value

Type: System.Object
The requested service, or nulla null reference (Nothing in Visual Basic) if the service could not be located.

Implements

IServiceProvider.GetService(Type)

Remarks

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

To get a specific Environment SDK 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;
    

Nota

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;

.NET Framework Security

See Also

Reference

ServiceProvider Class

ServiceProvider Members

GetService Overload

Microsoft.VisualStudio.Shell Namespace