Type.GetTypeFromCLSID Type.GetTypeFromCLSID Type.GetTypeFromCLSID Type.GetTypeFromCLSID Method

定義

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

多載

GetTypeFromCLSID(Guid, String, Boolean) GetTypeFromCLSID(Guid, String, Boolean) GetTypeFromCLSID(Guid, String, Boolean) 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) GetTypeFromCLSID(Guid, String) GetTypeFromCLSID(Guid, String) GetTypeFromCLSID(Guid, String)

從指定的伺服器中,取得與指定的類別識別項 (CLSID) 相關聯的類型。Gets the type associated with the specified class identifier (CLSID) from the specified server.

GetTypeFromCLSID(Guid, Boolean) GetTypeFromCLSID(Guid, Boolean) GetTypeFromCLSID(Guid, Boolean) 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) GetTypeFromCLSID(Guid) GetTypeFromCLSID(Guid) GetTypeFromCLSID(Guid)

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

GetTypeFromCLSID(Guid, String, Boolean) GetTypeFromCLSID(Guid, String, Boolean) GetTypeFromCLSID(Guid, String, Boolean) 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 Guid Guid Guid

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

server
String String String 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 Boolean Boolean 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 應用程式,從名為 computer17.central.contoso.com 伺服器 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方法,並將它關閉藉由呼叫Application.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 物件從.NET Framework 應用程式時您知道 COM 物件的類別識別項 (CLSID)。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方法以晚期繫結存取 COM 物件程式設計識別項 (ProgID) 您知道。You can call the GetTypeFromProgID method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

具現化 unmanaged 的 COM 物件的 CLSID 是兩步驟程序: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指定時就會擲回truethrowOnError,但不是會取消註冊的 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) 由 guidGUID屬性。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)方法會傳回Type物件,對應至特定的受管理物件的 GUID 不能使用該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.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11][!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11] 相反地,GetTypeFromCLSID(Guid, String, Boolean)只應該用來擷取的 unmanaged 的 COM 物件,與產生的 GUIDType物件傳遞至CreateInstance(Type)方法必須代表 unmanaged 的 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) GetTypeFromCLSID(Guid, String) GetTypeFromCLSID(Guid, String) 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 Guid Guid Guid

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

server
String String String 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.

傳回

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

範例

下列範例會使用 Microsoft Word 的 CLSID應用程式物件擷取代表 Microsoft Word 應用程式,從名為 computer17.central.contoso.com 伺服器 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方法,並將它關閉藉由呼叫Application.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 物件從.NET Framework 應用程式時您知道 COM 物件的類別識別項 (CLSID)。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方法以晚期繫結存取 COM 物件程式設計識別項 (ProgID) 您知道。You can call the GetTypeFromProgID method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

具現化 unmanaged 的 COM 物件的 CLSID 是兩步驟程序: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) 由 guidGUID屬性。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 的物件,您不能使用該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.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11][!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11] 相反地,GetTypeFromCLSID(Guid, String, Boolean)只應該用來擷取的 unmanaged 的 COM 物件,與產生的 GUIDType物件傳遞至CreateInstance(Type)方法必須代表 unmanaged 的 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) GetTypeFromCLSID(Guid, Boolean) GetTypeFromCLSID(Guid, Boolean) 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 Guid Guid Guid

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

throwOnError
Boolean Boolean Boolean 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方法,並將它關閉藉由呼叫Application.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 物件從.NET Framework 應用程式時您知道 COM 物件的類別識別項 (CLSID)。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方法以晚期繫結存取 COM 物件程式設計識別項 (ProgID) 您知道。You can call the GetTypeFromProgID method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

具現化 unmanaged 的 COM 物件的 CLSID 是兩步驟程序: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指定時就會擲回truethrowOnError,但不是會取消註冊的 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) 由 guidGUID屬性。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 的物件,您不能使用該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.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11][!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11] 相反地,GetTypeFromCLSID(Guid, String, Boolean)只應該用來擷取的 unmanaged 的 COM 物件,與產生的 GUIDType物件傳遞至CreateInstance(Type)方法必須代表 unmanaged 的 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) GetTypeFromCLSID(Guid) GetTypeFromCLSID(Guid) 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 Guid Guid 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方法,並將它關閉藉由呼叫Application.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 物件從.NET Framework 應用程式時您知道 COM 物件的類別識別項 (CLSID)。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方法以晚期繫結存取 COM 物件程式設計識別項 (ProgID) 您知道。You can call the GetTypeFromProgID method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

具現化 unmanaged 的 COM 物件的 CLSID 是兩步驟程序: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)多載會忽略任何會具現化時,可能會發生例外狀況Type物件,根據clsid引數。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) 由 guidGUID屬性。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 的物件,您不能使用該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.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11][!code-csharpSystem.Type.GetTypeFromCLSID#11] [!code-vbSystem.Type.GetTypeFromCLSID#11] 相反地,GetTypeFromCLSID(Guid, String, Boolean)只應該用來擷取的 unmanaged 的 COM 物件,與產生的 GUIDType物件傳遞至CreateInstance(Type)方法必須代表 unmanaged 的 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.

適用於