Type.GetTypeFromCLSID 方法

定义

获取与指定类标识符 (CLSID) 关联的类型。Gets the type associated with the specified class identifier (CLSID).

重载

GetTypeFromCLSID(Guid, String, Boolean)

从指定服务器获取与指定类标识符 (CLSID) 关联的类型,指定在加载该类型时如果发生错误是否引发异常。Gets the type associated with the specified class identifier (CLSID) from the specified server, specifying whether to throw an exception if an error occurs while loading the type.

GetTypeFromCLSID(Guid, String)

从指定服务器获取与指定类标识符 (CLSID) 关联的类型。Gets the type associated with the specified class identifier (CLSID) from the specified server.

GetTypeFromCLSID(Guid, Boolean)

获取与指定类标识符 (CLSID) 关联的类型,指定在加载该类型时如果发生错误是否引发异常。Gets the type associated with the specified class identifier (CLSID), specifying whether to throw an exception if an error occurs while loading the type.

GetTypeFromCLSID(Guid)

获取与指定类标识符 (CLSID) 关联的类型。Gets the type associated with the specified class identifier (CLSID).

GetTypeFromCLSID(Guid, String, Boolean)

从指定服务器获取与指定类标识符 (CLSID) 关联的类型,指定在加载该类型时如果发生错误是否引发异常。Gets the type associated with the specified class identifier (CLSID) from the specified server, specifying whether to throw an exception if an error occurs while loading the type.

public:
 static Type ^ GetTypeFromCLSID(Guid clsid, System::String ^ server, bool throwOnError);
public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError);
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。The CLSID of the type to get.

server
String

用于从中加载该类型的服务器。The server from which to load the type. 如果服务器名称为 null,则此方法会自动恢复到本地计算机上。If the server name is null, this method automatically reverts to the local machine.

throwOnError
Boolean

true 将引发所发生的任何异常。true to throw any exception that occurs.

-or- false 将忽略所发生的任何异常。false to ignore any exception that occurs.

返回

System.__ComObject,无论 CLSID 是否有效。System.__ComObject regardless of whether the CLSID is valid.

示例

下面的示例使用 Microsoft Word应用程序对象的 CLSID 从名为 computer17.central.contoso.com 的服务器中检索代表 Microsoft Word 应用程序的 COM 类型。The following example uses the CLSID of the Microsoft Word Application object to retrieve a COM type that represents the Microsoft Word application from a server named computer17.central.contoso.com. 然后通过调用Activator.CreateInstance方法来实例化该类型,并通过调用应用程序 Quit方法关闭该类型。It then instantiates the type by calling the Activator.CreateInstance method, and closes it by calling the Application.Quit method. 如果在加载该类型时发生错误,则会引发异常。An exception is thrown if an error occurs while loading the type.

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 对象进行后期绑定访问。The GetTypeFromCLSID method supports late-bound access to unmanaged COM objects from .NET Framework apps when you know the COM object's class identifier (CLSID). COM 类的类标识符是在注册表的 HKEY_CLASSES_ROOT\CLSID 项中定义的。The class identifier for COM classes is defined in the HKEY_CLASSES_ROOT\CLSID key of the registry. 可以检索IsCOMObject属性的值,以确定此方法返回的类型是否为 COM 对象。You can retrieve the value of the IsCOMObject property to determine whether the type returned by this method is a COM object.

提示

您可以调用GetTypeFromProgID方法来对您知道其编程标识符(ProgID)的 COM 对象进行后期绑定访问。You can call the GetTypeFromProgID method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

从其 CLSID 实例化非托管 COM 对象的过程分为两个步骤:Instantiating an unmanaged COM object from its CLSID is a two-step process:

  1. 通过调用Type __ComObject 方法来获取一个对象,该对象表示与CLSIDGetTypeFromCLSID相对应的。Get a Type object that represents the __ComObject that corresponds to the CLSID by calling the GetTypeFromCLSID method.

  2. Activator.CreateInstance(Type)调用方法来实例化 COM 对象。Call the Activator.CreateInstance(Type) method to instantiate the COM object.

当指定OutOfMemoryException true时,将引发诸如之类的throwOnError异常,但未注册的 clsid 将不会失败。Exceptions such as OutOfMemoryException will be thrown when specifying true for throwOnError, but it will not fail for unregistered CLSIDs.

调用方说明

此方法用于处理 COM 对象,而不是与 .NET Framework 对象一起使用。This method is intended for use when working with COM objects, not with .NET Framework objects. 所有托管对象(包括对 COM 可见的对象,即其ComVisibleAttribute属性为true)都具有由GUID属性返回的 GUID。All managed objects, including those that are visible to COM (that is, their ComVisibleAttribute attribute is true) have a GUID that is returned by the GUID property. 尽管方法返回对应于特定托管对象的 GUID CreateInstance(Type)Type 对象,但你不能使用该对象通过调用方法来创建类型实例,如Type以下 GetTypeFromCLSID(Guid, String, Boolean)示例所示展示.Although the GetTypeFromCLSID(Guid, String, Boolean) method returns a Type object that corresponds to the GUID for a particular managed object, you can't use that Type object to create a type instance by calling the CreateInstance(Type) method, as the following example shows.

[!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11][!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11] 相反, GetTypeFromCLSID(Guid, String, Boolean)只应使用来检索非托管 com 对象的 GUID,并且传递给该CreateInstance(Type)方法的Type生成对象必须表示非托管 com 对象。Instead, the GetTypeFromCLSID(Guid, String, Boolean) should only be used to retrieve the GUID of an unmanaged COM object, and the resulting Type object that is passed to the CreateInstance(Type) method must represent an unmanaged COM object.

GetTypeFromCLSID(Guid, String)

从指定服务器获取与指定类标识符 (CLSID) 关联的类型。Gets the type associated with the specified class identifier (CLSID) from the specified server.

public:
 static Type ^ GetTypeFromCLSID(Guid clsid, System::String ^ server);
public static Type GetTypeFromCLSID (Guid clsid, string server);
static member GetTypeFromCLSID : Guid * string -> Type
Public Shared Function GetTypeFromCLSID (clsid As Guid, server As String) As Type

参数

clsid
Guid

要获取的类型的 CLSID。The CLSID of the type to get.

server
String

用于从中加载该类型的服务器。The server from which to load the type. 如果服务器名称为 null,则此方法会自动恢复到本地计算机上。If the server name is null, this method automatically reverts to the local machine.

返回

System.__ComObject,无论 CLSID 是否有效。System.__ComObject regardless of whether the CLSID is valid.

示例

下面的示例使用 Microsoft Word应用程序对象的 CLSID 从名为 computer17.central.contoso.com 的服务器中检索代表 Microsoft Word 应用程序的 COM 类型。The following example uses the CLSID of the Microsoft Word Application object to retrieve a COM type that represents the Microsoft Word application from a server named computer17.central.contoso.com. 然后通过调用Activator.CreateInstance方法来实例化该类型,并通过调用应用程序 Quit方法关闭该类型。It then instantiates the type by calling the Activator.CreateInstance method, and closes it by calling the Application.Quit method.

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

注解

GetTypeFromCLSID你知道 COM 对象的类标识符(CLSID)时,该方法支持对 .NET Framework 应用程序的非托管 COM 对象进行后期绑定访问。The GetTypeFromCLSID method supports late-bound access to unmanaged COM objects from .NET Framework apps when you know the COM object's class identifier (CLSID). COM 类的类标识符是在注册表的 HKEY_CLASSES_ROOT\CLSID 项中定义的。The class identifier for COM classes is defined in the HKEY_CLASSES_ROOT\CLSID key of the registry. 可以检索IsCOMObject属性的值,以确定此方法返回的类型是否为 COM 对象。You can retrieve the value of the IsCOMObject property to determine whether the type returned by this method is a COM object.

提示

您可以调用GetTypeFromProgID方法来对您知道其编程标识符(ProgID)的 COM 对象进行后期绑定访问。You can call the GetTypeFromProgID method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

从其 CLSID 实例化非托管 COM 对象的过程分为两个步骤:Instantiating an unmanaged COM object from its CLSID is a two-step process:

  1. 通过调用Type __ComObject 方法来获取一个对象,该对象表示与CLSIDGetTypeFromCLSID相对应的。Get a Type object that represents the __ComObject that corresponds to the CLSID by calling the GetTypeFromCLSID method.

  2. Activator.CreateInstance(Type)调用方法来实例化 COM 对象。Call the Activator.CreateInstance(Type) method to instantiate the COM object.

调用方说明

此方法用于处理 COM 对象,而不是与 .NET Framework 对象一起使用。This method is intended for use when working with COM objects, not with .NET Framework objects. 所有托管对象(包括对 COM 可见的对象,即其ComVisibleAttribute属性为true)都具有由GUID属性返回的 GUID。All managed objects, including those that are visible to COM (that is, their ComVisibleAttribute attribute is true) have a GUID that is returned by the GUID property. 尽管方法返回Type的对象与 .NET Framework 对象的 GUID 对应,但无法通过调用CreateInstance(Type)方法来使用Type该对象创建类型实例,如下面的示例所示。Although the method returns a Type object that corresponds to the GUID for .NET Framework objects, you can't use that Type object to create a type instance by calling the CreateInstance(Type) method, as the following example shows.

[!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11][!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11] 相反, GetTypeFromCLSID(Guid, String, Boolean)只应使用来检索非托管 com 对象的 GUID,并且传递给该CreateInstance(Type)方法的Type生成对象必须表示非托管 com 对象。Instead, the GetTypeFromCLSID(Guid, String, Boolean) should only be used to retrieve the GUID of an unmanaged COM object, and the resulting Type object that is passed to the CreateInstance(Type) method must represent an unmanaged COM object.

GetTypeFromCLSID(Guid, Boolean)

获取与指定类标识符 (CLSID) 关联的类型,指定在加载该类型时如果发生错误是否引发异常。Gets the type associated with the specified class identifier (CLSID), specifying whether to throw an exception if an error occurs while loading the type.

public:
 static Type ^ GetTypeFromCLSID(Guid clsid, bool throwOnError);
public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError);
static member GetTypeFromCLSID : Guid * bool -> Type
Public Shared Function GetTypeFromCLSID (clsid As Guid, throwOnError As Boolean) As Type

参数

clsid
Guid

要获取的类型的 CLSID。The CLSID of the type to get.

throwOnError
Boolean

true 将引发所发生的任何异常。true to throw any exception that occurs.

-or- false 将忽略所发生的任何异常。false to ignore any exception that occurs.

返回

System.__ComObject,无论 CLSID 是否有效。System.__ComObject regardless of whether the CLSID is valid.

示例

下面的示例使用 Microsoft Word应用程序对象的 CLSID 来检索表示 Microsoft word 应用程序的 COM 类型。The following example uses the CLSID of the Microsoft Word Application object to retrieve a COM type that represents the Microsoft Word application. 然后通过调用Activator.CreateInstance方法来实例化该类型,并通过调用应用程序 Quit方法关闭该类型。It then instantiates the type by calling the Activator.CreateInstance method, and closes it by calling the Application.Quit method. 如果在加载该类型时发生错误,则会引发异常。An exception is thrown if an error occurs while loading the type.

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

注解

GetTypeFromCLSID你知道 COM 对象的类标识符(CLSID)时,该方法支持对 .NET Framework 应用程序的非托管 COM 对象进行后期绑定访问。The GetTypeFromCLSID method supports late-bound access to unmanaged COM objects from .NET Framework apps when you know the COM object's class identifier (CLSID). COM 类的类标识符是在注册表的 HKEY_CLASSES_ROOT\CLSID 项中定义的。The class identifier for COM classes is defined in the HKEY_CLASSES_ROOT\CLSID key of the registry. 可以检索IsCOMObject属性的值,以确定此方法返回的类型是否为 COM 对象。You can retrieve the value of the IsCOMObject property to determine whether the type returned by this method is a COM object.

提示

您可以调用GetTypeFromProgID方法来对您知道其编程标识符(ProgID)的 COM 对象进行后期绑定访问。You can call the GetTypeFromProgID method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

从其 CLSID 实例化非托管 COM 对象的过程分为两个步骤:Instantiating an unmanaged COM object from its CLSID is a two-step process:

  1. 通过调用Type __ComObject 方法来获取一个对象,该对象表示与CLSIDGetTypeFromCLSID相对应的。Get a Type object that represents the __ComObject that corresponds to the CLSID by calling the GetTypeFromCLSID method.

  2. Activator.CreateInstance(Type)调用方法来实例化 COM 对象。Call the Activator.CreateInstance(Type) method to instantiate the COM object.

有关说明,请参阅示例。See the example for an illustration.

当指定OutOfMemoryException true时,将引发诸如之类的throwOnError异常,但未注册的 clsid 将不会失败。Exceptions such as OutOfMemoryException will be thrown when specifying true for throwOnError, but it will not fail for unregistered CLSIDs.

调用方说明

此方法用于处理 COM 对象,而不是与 .NET Framework 对象一起使用。This method is intended for use when working with COM objects, not with .NET Framework objects. 所有托管对象(包括对 COM 可见的对象,即其ComVisibleAttribute属性为true)都具有由GUID属性返回的 GUID。All managed objects, including those that are visible to COM (that is, their ComVisibleAttribute attribute is true) have a GUID that is returned by the GUID property. 尽管方法返回Type的对象与 .NET Framework 对象的 GUID 对应,但无法通过调用CreateInstance(Type)方法来使用Type该对象创建类型实例,如下面的示例所示。Although the method returns a Type object that corresponds to the GUID for .NET Framework objects, you can't use that Type object to create a type instance by calling the CreateInstance(Type) method, as the following example shows.

[!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11][!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11] 相反, GetTypeFromCLSID(Guid, String, Boolean)只应使用来检索非托管 com 对象的 GUID,并且传递给该CreateInstance(Type)方法的Type生成对象必须表示非托管 com 对象。Instead, the GetTypeFromCLSID(Guid, String, Boolean) should only be used to retrieve the GUID of an unmanaged COM object, and the resulting Type object that is passed to the CreateInstance(Type) method must represent an unmanaged COM object.

GetTypeFromCLSID(Guid)

获取与指定类标识符 (CLSID) 关联的类型。Gets the type associated with the specified class identifier (CLSID).

public:
 static Type ^ GetTypeFromCLSID(Guid clsid);
public static Type GetTypeFromCLSID (Guid clsid);
static member GetTypeFromCLSID : Guid -> Type
Public Shared Function GetTypeFromCLSID (clsid As Guid) As Type

参数

clsid
Guid

要获取的类型的 CLSID。The CLSID of the type to get.

返回

System.__ComObject,无论 CLSID 是否有效。System.__ComObject regardless of whether the CLSID is valid.

示例

下面的示例使用 Microsoft Word应用程序对象的 CLSID 来检索表示 Microsoft word 应用程序的 COM 类型。The following example uses the CLSID of the Microsoft Word Application object to retrieve a COM type that represents the Microsoft Word application. 然后通过调用Activator.CreateInstance方法来实例化该类型,并通过调用应用程序 Quit方法关闭该类型。It then instantiates the type by calling the Activator.CreateInstance method, and closes it by calling the Application.Quit method.

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

注解

GetTypeFromCLSID你知道 COM 对象的类标识符(CLSID)时,该方法支持对 .NET Framework 应用程序的非托管 COM 对象进行后期绑定访问。The GetTypeFromCLSID method supports late-bound access to unmanaged COM objects from .NET Framework apps when you know the COM object's class identifier (CLSID). COM 类的类标识符是在注册表的 HKEY_CLASSES_ROOT\CLSID 项中定义的。The class identifier for COM classes is defined in the HKEY_CLASSES_ROOT\CLSID key of the registry. 可以检索IsCOMObject属性的值,以确定此方法返回的类型是否为 COM 对象。You can retrieve the value of the IsCOMObject property to determine whether the type returned by this method is a COM object.

提示

您可以调用GetTypeFromProgID方法来对您知道其编程标识符(ProgID)的 COM 对象进行后期绑定访问。You can call the GetTypeFromProgID method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

从其 CLSID 实例化非托管 COM 对象的过程分为两个步骤:Instantiating an unmanaged COM object from its CLSID is a two-step process:

  1. 通过调用Type __ComObject 方法来获取一个对象,该对象表示与CLSIDGetTypeFromCLSID相对应的。Get a Type object that represents the__ComObject that corresponds to the CLSID by calling the GetTypeFromCLSID method.

  2. Activator.CreateInstance(Type)调用方法来实例化 COM 对象。Call the Activator.CreateInstance(Type) method to instantiate the COM object.

有关说明,请参阅示例。See the example for an illustration.

重载GetTypeFromCLSID(Guid)将忽略Typeclsid基于参数实例化对象时可能发生的任何异常。The GetTypeFromCLSID(Guid) overload ignores any exception that may occur when instantiating a Type object based on the clsid argument. 请注意,如果clsid在注册表中找不到,则不会引发异常。Note that no exception is thrown if clsid is not found in the registry.

调用方说明

此方法用于处理 COM 对象,而不是与 .NET Framework 对象一起使用。This method is intended for use when working with COM objects, not with .NET Framework objects. 所有托管对象(包括对 COM 可见的对象,即其ComVisibleAttribute属性为true)都具有由GUID属性返回的 GUID。All managed objects, including those that are visible to COM (that is, their ComVisibleAttribute attribute is true) have a GUID that is returned by the GUID property. 尽管方法返回Type的对象与 .NET Framework 对象的 GUID 对应,但无法通过调用CreateInstance(Type)方法来使用Type该对象创建类型实例,如下面的示例所示。Although the method returns a Type object that corresponds to the GUID for .NET Framework objects, you can't use that Type object to create a type instance by calling the CreateInstance(Type) method, as the following example shows.

[!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11][!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11] 相反, GetTypeFromCLSID(Guid, String, Boolean)只应使用来检索非托管 com 对象的 GUID,并且传递给该CreateInstance(Type)方法的Type生成对象必须表示非托管 com 对象。Instead, the GetTypeFromCLSID(Guid, String, Boolean) should only be used to retrieve the GUID of an unmanaged COM object, and the resulting Type object that is passed to the CreateInstance(Type) method must represent an unmanaged COM object.

适用于