Type.GetTypeFromCLSID Method

Definition

取得與指定的類別識別項 (CLSID) 關聯的類型。Gets the type associated with the specified class identifier (CLSID).

Overloads

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

Parameters

clsid
Guid

要取得之類型的 CLSID。The CLSID of the type to get.

server
String

要載入類型的伺服器。The server from which to load the type. 如果伺服器名稱為 null,此方法將會自動還原成本機電腦 (Local Machine)。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.

Returns

Type

System.__ComObject (不論 CLSID 是否有效)。System.__ComObject regardless of whether the CLSID is valid.

Examples

下列範例會使用 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 方法來具現化型別,並藉由呼叫應用程式結束方法來關閉它。It then instantiates the type by calling the Activator.CreateInstance method, and closes it by calling the Application.Quit method. 如果載入類型時發生錯誤,則會擲回例外狀況(exception)。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

Remarks

當您知道 COM 物件的類別識別碼(CLSID)時,GetTypeFromCLSID 方法可支援從 .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.

Tip

您可以呼叫 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. 藉由呼叫 GetTypeFromCLSID 方法,取得代表對應至 CLSID 之 __ComObjectType 物件。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.

指定 throwOnErrortrue 時,將會擲回 OutOfMemoryException 之類的例外狀況,但未註冊的 Clsid 則不會失敗。Exceptions such as OutOfMemoryException will be thrown when specifying true for throwOnError, but it will not fail for unregistered CLSIDs.

Notes to Callers

這個方法適用于使用 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. 雖然 GetTypeFromCLSID(Guid, String, Boolean) 方法會傳回對應至特定 managed 物件之 GUID 的 Type 物件,但是您無法使用該 Type 物件,藉由呼叫 CreateInstance(Type) 方法來建立類型實例,如下列範例所示。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.web GetTypeFromCLSID # 11][! code-vbSystem.web 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

Parameters

clsid
Guid

要取得之類型的 CLSID。The CLSID of the type to get.

server
String

要載入類型的伺服器。The server from which to load the type. 如果伺服器名稱為 null,此方法將會自動還原成本機電腦 (Local Machine)。If the server name is null, this method automatically reverts to the local machine.

Returns

Type

System.__ComObject (不論 CLSID 是否有效)。System.__ComObject regardless of whether the CLSID is valid.

Examples

下列範例會使用 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 方法來具現化型別,並藉由呼叫應用程式結束方法來關閉它。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

Remarks

當您知道 COM 物件的類別識別碼(CLSID)時,GetTypeFromCLSID 方法可支援從 .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.

Tip

您可以呼叫 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. 藉由呼叫 GetTypeFromCLSID 方法,取得代表對應至 CLSID 之 __ComObjectType 物件。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.

Notes to Callers

這個方法適用于使用 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. 雖然方法會傳回對應至 .NET Framework 物件之 GUID 的 Type 物件,但是您無法使用該 Type 物件,藉由呼叫 CreateInstance(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.web GetTypeFromCLSID # 11][! code-vbSystem.web 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

Parameters

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.

Returns

Type

System.__ComObject (不論 CLSID 是否有效)。System.__ComObject regardless of whether the CLSID is valid.

Examples

下列範例會使用 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 方法來具現化型別,並藉由呼叫應用程式結束方法來關閉它。It then instantiates the type by calling the Activator.CreateInstance method, and closes it by calling the Application.Quit method. 如果載入類型時發生錯誤,則會擲回例外狀況(exception)。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

Remarks

當您知道 COM 物件的類別識別碼(CLSID)時,GetTypeFromCLSID 方法可支援從 .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.

Tip

您可以呼叫 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. 藉由呼叫 GetTypeFromCLSID 方法,取得代表對應至 CLSID 之 __ComObjectType 物件。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.

指定 throwOnErrortrue 時,將會擲回 OutOfMemoryException 之類的例外狀況,但未註冊的 Clsid 則不會失敗。Exceptions such as OutOfMemoryException will be thrown when specifying true for throwOnError, but it will not fail for unregistered CLSIDs.

Notes to Callers

這個方法適用于使用 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. 雖然方法會傳回對應至 .NET Framework 物件之 GUID 的 Type 物件,但是您無法使用該 Type 物件,藉由呼叫 CreateInstance(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.web GetTypeFromCLSID # 11][! code-vbSystem.web 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

Parameters

clsid
Guid

要取得之類型的 CLSID。The CLSID of the type to get.

Returns

Type

System.__ComObject (不論 CLSID 是否有效)。System.__ComObject regardless of whether the CLSID is valid.

Examples

下列範例會使用 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 方法來具現化型別,並藉由呼叫應用程式結束方法來關閉它。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

Remarks

當您知道 COM 物件的類別識別碼(CLSID)時,GetTypeFromCLSID 方法可支援從 .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.

Tip

您可以呼叫 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. 藉由呼叫 GetTypeFromCLSID 方法,取得代表對應至 CLSID 之__ComObjectType 物件。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) 多載會忽略根據 clsid 引數具現化 Type 物件時可能發生的任何例外狀況。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.

Notes to Callers

這個方法適用于使用 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. 雖然方法會傳回對應至 .NET Framework 物件之 GUID 的 Type 物件,但是您無法使用該 Type 物件,藉由呼叫 CreateInstance(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.web GetTypeFromCLSID # 11][! code-vbSystem.web 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.

Applies to