Share via


Package.GetService 方法

gets 类型根据 VSPackage 服务容器的服务。

命名空间:  Microsoft.VisualStudio.Shell
程序集:  Microsoft.VisualStudio.Shell.11.0(在 Microsoft.VisualStudio.Shell.11.0.dll 中)

语法

声明
Protected Function GetService ( _
    serviceType As Type _
) As Object
protected Object GetService(
    Type serviceType
)

参数

  • serviceType
    类型:System.Type
    要检索的服务类型。

返回值

类型:System.Object
所请求的服务类型实例,如果未能找到服务,则为 nullnull 引用(在 Visual Basic 中为 Nothing)。

异常

异常 条件
ArgumentNullException

serviceType 为 nullnull 引用(在 Visual Basic 中为 Nothing)。

备注

托管 Vspackage 可以通过查询 SDK 的互操作程序集使用 GetService 获取环境 SDK COM 接口。

获取特定环境 SDK 接口:

  1. 应调用GetService 与 serviceType 返回使用该接口为 typeof的参数。

  2. 必须将 GetService 的返回值赋给接口类型。

,因为 GetService 搜索与 IUnknown,的接口类型的基类型服务类型 GUID 的强制转换是必需的。

例如,可以获取一 IVsUIShell 接口与:

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

说明说明

在以前的原因, IVsTextManager 接口不能按照这种方式获取。若要获取 IVsTextManager 接口,第一个使用 VsTextManagerClass (实现接口) 的类 (如) 作为参数传递给 typeof,然后将 GetService 的返回值。 IVsTextManager,例如:IVsTextManager mytext_mgr = myPackage.GetService(System.typeof(VsTextManagerClass)) as IVsTextManager;

.NET Framework 安全性

请参见

参考

Package 类

Microsoft.VisualStudio.Shell 命名空间

IServiceProvider

IVsTextManager

Type

ArgumentNullException