Type.GetTypeFromCLSID 方法

定义

获取与指定类标识符 (CLSID) 关联的类型。

重载

GetTypeFromCLSID(Guid, String, Boolean)

从指定服务器获取与指定类标识符 (CLSID) 关联的类型,指定在加载该类型时如果发生错误是否引发异常。

GetTypeFromCLSID(Guid, String)

从指定服务器获取与指定类标识符 (CLSID) 关联的类型。

GetTypeFromCLSID(Guid, Boolean)

获取与指定类标识符 (CLSID) 关联的类型,指定在加载该类型时如果发生错误是否引发异常。

GetTypeFromCLSID(Guid)

获取与指定类标识符 (CLSID) 关联的类型。

GetTypeFromCLSID(Guid, String, Boolean)

从指定服务器获取与指定类标识符 (CLSID) 关联的类型,指定在加载该类型时如果发生错误是否引发异常。

public:
 static Type ^ GetTypeFromCLSID(Guid clsid, System::String ^ server, bool throwOnError);
public static Type? GetTypeFromCLSID (Guid clsid, string? server, bool throwOnError);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID (Guid clsid, string? server, bool throwOnError);
public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError);
static member GetTypeFromCLSID : Guid * string * bool -> Type
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member GetTypeFromCLSID : Guid * string * bool -> Type
Public Shared Function GetTypeFromCLSID (clsid As Guid, server As String, throwOnError As Boolean) As Type

参数

clsid
Guid

要获取的类型的 CLSID。

server
String

用于从中加载该类型的服务器。 如果服务器名称为 null,则此方法会自动恢复到本地计算机上。

throwOnError
Boolean

true 将引发所发生的任何异常。

  • 或 - false 将忽略所发生的任何异常。

返回

Type

System.__ComObject,无论 CLSID 是否有效。

属性

示例

下面的示例使用 Microsoft Word应用程序对象的 CLSID 从名为 computer17.central.contoso.com 的服务器中检索表示 Microsoft Word 应用程序的 COM 类型。 然后通过调用方法来实例化该类型 Activator.CreateInstance ,并通过调用 应用程序 Quit 方法关闭该类型。 如果在加载该类型时发生错误,则会引发异常。

using System;
using System.Reflection;
using System.Runtime.InteropServices;

public class Example
{
   private const string WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}";
   
   public static void Main()
   {
      try {
         // Start an instance of the Word application.
         var word = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID), 
                                          "computer17.central.contoso.com",
                                          true);
         Console.WriteLine("Instantiated Type object from CLSID {0}",
                           WORD_CLSID);
         Object wordObj = Activator.CreateInstance(word);
         Console.WriteLine("Instantiated {0}", 
                           wordObj.GetType().FullName, WORD_CLSID);
            
         // Close Word.
         word.InvokeMember("Quit", BindingFlags.InvokeMethod, null, 
                           wordObj, new object[] { 0, 0, false } );
      }
      // The method can throw any of a variety of exceptions.
      catch (Exception e) {
         Console.WriteLine("{0}: Unable to instantiate an object for {1}", 
                           e.GetType().Name, WORD_CLSID);
      }      
   }
}
// The example displays the following output:
//    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
//    Instantiated Microsoft.Office.Interop.Word.ApplicationClass
Imports System.Reflection
Imports System.Runtime.InteropServices

Module Example
   Private Const WORD_CLSID As String = "{000209FF-0000-0000-C000-000000000046}"
   
   Public Sub Main()
      Try
         ' Start an instance of the Word application.
         Dim word As Type = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID), 
                                                  "computer17.central.contoso.com",
                                                  True)
         Console.WriteLine("Instantiated Type object from CLSID {0}",
                           WORD_CLSID)

         Dim wordObj As Object = Activator.CreateInstance(word)
         Console.WriteLine("Instantiated {0}", 
                           wordObj.GetType().FullName)
         
         ' Close Word.
         word.InvokeMember("Quit", BindingFlags.InvokeMethod, Nothing, 
                           wordObj, New Object() { 0, 0, False } )
      ' The method can throw any of a variety of exceptions.
      Catch e As Exception
         Console.WriteLine("{0}: Unable to instantiate an object for {1}", 
                           e.GetType().Name, WORD_CLSID)
      End Try
   End Sub
End Module
' The example displays the following output:
'    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
'    Instantiated Microsoft.Office.Interop.Word.ApplicationClass

注解

GetTypeFromCLSID如果你知道 COM 对象的类标识符 (CLSID) ,则该方法支持对来自 .NET Framework 应用的非托管 com 对象进行后期绑定访问。 COM 类的类标识符是在注册表的 HKEY_CLASSES_ROOT\CLSID 项中定义的。 可以检索属性的值 IsCOMObject ,以确定此方法返回的类型是否为 COM 对象。

提示

你可以调用 GetTypeFromProgID 方法来对 COM 对象进行后期绑定访问,这些对象的编程标识符 (ProgID) 你知道的。

从其 CLSID 实例化非托管 COM 对象的过程分为两个步骤:

  1. Type通过调用方法来获取一个对象,该对象表示 __ComObject 与 CLSID 相对应的 GetTypeFromCLSID

  2. 调用 Activator.CreateInstance(Type) 方法来实例化 COM 对象。

OutOfMemoryException当指定时,将引发诸如之类 true 的异常 throwOnError ,但未注册的 clsid 将不会失败。

调用方说明

此方法用于处理 COM 对象,而不是与 .NET Framework 对象一起使用。 所有托管对象(包括对 COM 可见的对象 (即,其 ComVisibleAttribute 属性 true) 具有属性返回的 GUID GUID 。 尽管 GetTypeFromCLSID(Guid, String, Boolean) 方法返回对应于 Type 特定托管对象的 GUID 的对象,但你不能使用该 Type 对象通过调用方法来创建类型实例 CreateInstance(Type) ,如下面的示例所示。

::: code language = "csharp" source = "~/samples/snippets/csharp/VS_Snippets_CLR_System/system.type.gettypefromclsid/cs/gettypefromclsid11.cs" id = "Snippet11":::::: code language = "vb" source = "~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.type.gettypefromclsid/vb/gettypefromclsid11.vb" id = "Snippet11"::: 相反, GetTypeFromCLSID(Guid, String, Boolean) 只应使用来检索非托管 com 对象的 GUID,并且 Type 传递给该方法的生成对象 CreateInstance(Type) 必须表示非托管 com 对象。

适用于

GetTypeFromCLSID(Guid, String)

从指定服务器获取与指定类标识符 (CLSID) 关联的类型。

public:
 static Type ^ GetTypeFromCLSID(Guid clsid, System::String ^ server);
public static Type? GetTypeFromCLSID (Guid clsid, string? server);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID (Guid clsid, string? server);
public static Type GetTypeFromCLSID (Guid clsid, string server);
static member GetTypeFromCLSID : Guid * string -> Type
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member GetTypeFromCLSID : Guid * string -> Type
Public Shared Function GetTypeFromCLSID (clsid As Guid, server As String) As Type

参数

clsid
Guid

要获取的类型的 CLSID。

server
String

用于从中加载该类型的服务器。 如果服务器名称为 null,则此方法会自动恢复到本地计算机上。

返回

Type

System.__ComObject,无论 CLSID 是否有效。

属性

示例

以下示例使用 Microsoft Word Application对象的 CLSID 从名为 computer17.central.contoso.com 的服务器检索表示 Microsoft Word 应用程序的 COM 类型。 然后,它通过调用 方法实例化类型 Activator.CreateInstance ,然后通过调用 Application.Quit 方法关闭它。

using System;
using System.Reflection;
using System.Runtime.InteropServices;

public class Example
{
   private const string WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}";
   
   public static void Main()
   {
      // Start an instance of the Word application.
      var word = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID), "computer17.central.contoso.com");
      Console.WriteLine("Instantiated Type object from CLSID {0}",
                        WORD_CLSID);
      try {
         Object wordObj = Activator.CreateInstance(word);
         Console.WriteLine("Instantiated {0}", 
                           wordObj.GetType().FullName, WORD_CLSID);
            
         // Close Word.
         word.InvokeMember("Quit", BindingFlags.InvokeMethod, null, 
                           wordObj, new object[] { 0, 0, false } );
      }
      catch (COMException) {
         Console.WriteLine("Unable to instantiate object.");   
      }
   }
}
// The example displays the following output:
//    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
//    Instantiated Microsoft.Office.Interop.Word.ApplicationClass
Imports System.Reflection
Imports System.Runtime.InteropServices

Module Example
   Private Const WORD_CLSID As String = "{000209FF-0000-0000-C000-000000000046}"
   
   Public Sub Main()
      ' Start an instance of the Word application.
      Dim word As Type = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID), "computer17.central.contoso.com")
      Console.WriteLine("Instantiated Type object from CLSID {0}",
                        WORD_CLSID)
      Try
         Dim wordObj As Object = Activator.CreateInstance(word)
         Console.WriteLine("Instantiated {0}", 
                           wordObj.GetType().FullName)
         
         ' Close Word.
         word.InvokeMember("Quit", BindingFlags.InvokeMethod, Nothing, 
                           wordObj, New Object() { 0, 0, False } )
      Catch e As COMException
         Console.WriteLine("Unable to instantiate object.")
      End Try
   End Sub
End Module
' The example displays the following output:
'    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
'    Instantiated Microsoft.Office.Interop.Word.ApplicationClass

注解

当知道 COM 对象的类标识符.NET Framework CLSID (时, 方法支持从 .NET Framework 应用中对非托管 COM 对象进行后期 GetTypeFromCLSID) 。 COM 类的类标识符在注册表的HKEY_CLASSES_ROOT\CLSID项中定义。 可以检索 属性的值 IsCOMObject ,以确定此方法返回的类型是否是 COM 对象。

提示

可以调用 方法以后期绑定方式访问 COM 对象,这些 COM 对象的编程标识符 (GetTypeFromProgID ProgID) 您知道。

从其 CLSID 实例化非托管 COM 对象的过程包括两个步骤:

  1. 通过 Type 调用 方法获取表示 __ComObject 与 CLSID 相对应的 的 GetTypeFromCLSID 对象。

  2. 调用 Activator.CreateInstance(Type) 方法以实例化 COM 对象。

调用方说明

此方法适用于使用 COM 对象,而不是用于.NET Framework对象。 所有托管对象(包括 COM (的对象,即其属性) 具有由 属性返回的 ComVisibleAttribute true GUID GUID。 尽管 方法返回的对象对应于 .NET Framework 对象的 GUID,但无法通过调用 方法使用该对象来创建类型实例,如 Type Type CreateInstance(Type) 以下示例所示。

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.type.gettypefromclsid/cs/gettypefromclsid11.cs" id="Snippet11"::: ::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.type.gettypefromclsid/vb/gettypefromclsid11.vb" id="Snippet11":: 相反, 应仅用于检索非托管 COM 对象的 GUID,传递给 方法的结果对象必须表示非托管 GetTypeFromCLSID(Guid, String, Boolean) Type COM CreateInstance(Type) 对象。

适用于

GetTypeFromCLSID(Guid, Boolean)

获取与指定类标识符 (CLSID) 关联的类型,指定在加载该类型时如果发生错误是否引发异常。

public:
 static Type ^ GetTypeFromCLSID(Guid clsid, bool throwOnError);
public static Type? GetTypeFromCLSID (Guid clsid, bool throwOnError);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID (Guid clsid, bool throwOnError);
public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError);
static member GetTypeFromCLSID : Guid * bool -> Type
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member GetTypeFromCLSID : Guid * bool -> Type
Public Shared Function GetTypeFromCLSID (clsid As Guid, throwOnError As Boolean) As Type

参数

clsid
Guid

要获取的类型的 CLSID。

throwOnError
Boolean

true 将引发所发生的任何异常。

  • 或 - false 将忽略所发生的任何异常。

返回

Type

System.__ComObject,无论 CLSID 是否有效。

属性

示例

以下示例使用应用程序对象的 CLSID Microsoft Word检索表示应用程序Microsoft Word COM 类型。 然后,它通过调用 方法实例化类型 Activator.CreateInstance ,然后通过调用 Application.Quit 方法关闭它。 如果在加载类型时发生错误,则会引发异常。

using System;
using System.Reflection;
using System.Runtime.InteropServices;

public class Example
{
   private const string WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}";
   
   public static void Main()
   {
      try {
         // Start an instance of the Word application.
         var word = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID), true);
         Console.WriteLine("Instantiated Type object from CLSID {0}",
                           WORD_CLSID);
         Object wordObj = Activator.CreateInstance(word);
         Console.WriteLine("Instantiated {0}", 
                           wordObj.GetType().FullName, WORD_CLSID);
         
         // Close Word.
         word.InvokeMember("Quit", BindingFlags.InvokeMethod, null, 
                           wordObj, new object[] { 0, 0, false } );
      }
      catch (Exception) {
         Console.WriteLine("Unable to instantiate an object for {0}", WORD_CLSID);
      }
   }
}
// The example displays the following output:
//    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
//    Instantiated Microsoft.Office.Interop.Word.ApplicationClass
Imports System.Reflection
Imports System.Runtime.InteropServices

Module Example
   Private Const WORD_CLSID As String = "{000209FF-0000-0000-C000-000000000046}"
   
   Public Sub Main()
      ' Start an instance of the Word application.
      Try
         Dim word As Type = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID), True)
         Console.WriteLine("Instantiated Type object from CLSID {0}",
                           WORD_CLSID)
         Dim wordObj As Object = Activator.CreateInstance(word)
         Console.WriteLine("Instantiated {0}", 
                           wordObj.GetType().FullName)
         
         ' Close Word.
         word.InvokeMember("Quit", BindingFlags.InvokeMethod, Nothing, 
                           wordObj, New Object() { 0, 0, False } )
      ' The method can throw any of a number of unexpected exceptions.
      Catch e As Exception
         Console.WriteLine("Unable to instantiate an object for {0}", WORD_CLSID)
      End Try
   End Sub
End Module
' The example displays the following output:
'    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
'    Instantiated Microsoft.Office.Interop.Word.ApplicationClass

注解

当知道 COM 对象的类标识符.NET Framework CLSID (时, 方法支持从 .NET Framework 应用中对非托管 COM 对象进行后期 GetTypeFromCLSID) 。 COM 类的类标识符在注册表的HKEY_CLASSES_ROOT\CLSID项中定义。 可以检索 属性的值 IsCOMObject ,以确定此方法返回的类型是否是 COM 对象。

提示

可以调用 方法以后期绑定方式访问 COM 对象,这些 COM 对象的编程标识符 (GetTypeFromProgID ProgID) 您知道。

从其 CLSID 实例化非托管 COM 对象的过程包括两个步骤:

  1. 通过 Type 调用 方法获取表示 __ComObject 与 CLSID 相对应的 的 GetTypeFromCLSID 对象。

  2. 调用 Activator.CreateInstance(Type) 方法以实例化 COM 对象。

有关说明,请参阅示例。

为 指定 时将引发异常,但对于未注册的 OutOfMemoryException true throwOnError CLSID,不会失败。

调用方说明

此方法适用于使用 COM 对象,而不是用于.NET Framework对象。 所有托管对象(包括 COM (的对象,即其属性) 具有由 属性返回的 ComVisibleAttribute true GUID GUID。 尽管 方法返回的对象对应于 .NET Framework 对象的 GUID,但无法通过调用 方法使用该对象来创建类型实例,如 Type Type CreateInstance(Type) 以下示例所示。

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.type.gettypefromclsid/cs/gettypefromclsid11.cs" id="Snippet11"::: ::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.type.gettypefromclsid/vb/gettypefromclsid11.vb" id="Snippet11":: 相反, 应仅用于检索非托管 COM 对象的 GUID,传递给 方法的结果对象必须表示非托管 GetTypeFromCLSID(Guid, String, Boolean) Type COM CreateInstance(Type) 对象。

适用于

GetTypeFromCLSID(Guid)

获取与指定类标识符 (CLSID) 关联的类型。

public:
 static Type ^ GetTypeFromCLSID(Guid clsid);
public static Type? GetTypeFromCLSID (Guid clsid);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID (Guid clsid);
public static Type GetTypeFromCLSID (Guid clsid);
static member GetTypeFromCLSID : Guid -> Type
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
static member GetTypeFromCLSID : Guid -> Type
Public Shared Function GetTypeFromCLSID (clsid As Guid) As Type

参数

clsid
Guid

要获取的类型的 CLSID。

返回

Type

System.__ComObject,无论 CLSID 是否有效。

属性

示例

以下示例使用应用程序对象的 CLSID Microsoft Word检索表示应用程序Microsoft Word COM 类型。 然后,它通过调用 方法实例化类型 Activator.CreateInstance ,然后通过调用 Application.Quit 方法关闭它。

using System;
using System.Reflection;
using System.Runtime.InteropServices;

public class Example
{
   private const string WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}";
   
   public static void Main()
   {
      // Start an instance of the Word application.
      var word = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID));
      Console.WriteLine("Instantiated Type object from CLSID {0}",
                        WORD_CLSID);
      Object wordObj = Activator.CreateInstance(word);
      Console.WriteLine("Instantiated {0}", 
                        wordObj.GetType().FullName);
      
      // Close Word.
      word.InvokeMember("Quit", BindingFlags.InvokeMethod, null, 
                        wordObj, new object[] { 0, 0, false } );
   }
}
// The example displays the following output:
//    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
//    Instantiated Microsoft.Office.Interop.Word.ApplicationClass
Imports System.Reflection
Imports System.Runtime.InteropServices

Module Example
   Private Const WORD_CLSID As String = "{000209FF-0000-0000-C000-000000000046}"
   
   Public Sub Main()
      ' Start an instance of the Word application.
      Dim word As Type = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID))
      Console.WriteLine("Instantiated Type object from CLSID {0}",
                        WORD_CLSID)
      Dim wordObj As Object = Activator.CreateInstance(word)
      Console.WriteLine("Instantiated {0}", 
                        wordObj.GetType().FullName)
      
      ' Close Word.
      word.InvokeMember("Quit", BindingFlags.InvokeMethod, Nothing, 
                        wordObj, New Object() { 0, 0, False } )
   End Sub
End Module
' The example displays the following output:
'    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
'    Instantiated Microsoft.Office.Interop.Word.ApplicationClass

注解

当知道 COM 对象的类标识符.NET Framework CLSID (时, 方法支持从 .NET Framework 应用中对非托管 COM 对象进行后期 GetTypeFromCLSID) 。 COM 类的类标识符在注册表的HKEY_CLASSES_ROOT\CLSID项中定义。 可以检索 属性的值 IsCOMObject ,以确定此方法返回的类型是否是 COM 对象。

提示

可以调用 方法以后期绑定方式访问 COM 对象,这些 COM 对象的编程标识符 (GetTypeFromProgID ProgID) 您知道。

从其 CLSID 实例化非托管 COM 对象的过程包括两个步骤:

  1. 通过 Type 调用 方法获取表示 __ComObject 与 CLSID 相对应的 的 GetTypeFromCLSID 对象。

  2. 调用 Activator.CreateInstance(Type) 方法以实例化 COM 对象。

有关说明,请参阅示例。

GetTypeFromCLSID(Guid)重载忽略基于 参数实例化 Type 对象时可能会发生的任何 clsid 异常。 请注意,如果在注册表中找不到 clsid ,则不引发异常。

调用方说明

此方法适用于使用 COM 对象,而不是用于.NET Framework对象。 所有托管对象(包括 COM (的对象,即其属性) 具有由 属性返回的 ComVisibleAttribute true GUID GUID。 尽管 方法返回的对象对应于 .NET Framework 对象的 GUID,但无法通过调用 方法使用该对象来创建类型实例,如 Type Type CreateInstance(Type) 以下示例所示。

:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.type.gettypefromclsid/cs/gettypefromclsid11.cs" id="Snippet11"::: ::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.type.gettypefromclsid/vb/gettypefromclsid11.vb" id="Snippet11":: 相反, 应仅用于检索非托管 COM 对象的 GUID,传递给 方法的结果对象必须表示非托管 GetTypeFromCLSID(Guid, String, Boolean) Type COM CreateInstance(Type) 对象。

适用于