Type.GetTypeFromCLSID 方法

定義

取得與指定的類別識別項 (CLSID) 關聯的類型。

多載

GetTypeFromCLSID(Guid)

取得與指定的類別識別項 (CLSID) 關聯的類型。

GetTypeFromCLSID(Guid, Boolean)

取得與指定的類別識別項 (CLSID) 關聯的類型,並指定如果載入類型時發生錯誤是否擲回例外狀況。

GetTypeFromCLSID(Guid, String)

從指定的伺服器中,取得與指定的類別識別項 (CLSID) 相關聯的類型。

GetTypeFromCLSID(Guid, String, Boolean)

從指定的伺服器中,取得與指定的類別識別項 (CLSID) 相關聯的類型,並指定如果在載入類型時發生錯誤是否擲回例外狀況。

GetTypeFromCLSID(Guid)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

取得與指定的類別識別項 (CLSID) 關聯的類型。

C#
public static Type? GetTypeFromCLSID (Guid clsid);
C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID (Guid clsid);
C#
public static Type GetTypeFromCLSID (Guid clsid);

參數

clsid
Guid

要取得之類型的 CLSID。

傳回

System.__ComObject (不論 CLSID 是否有效)。

屬性

範例

下列範例會使用 Microsoft Word Application 物件的 CLSID 來擷取代表 Microsoft Word 應用程式的 COM 類型。 然後它會呼叫 Activator.CreateInstance 方法來具現化類型,並藉由呼叫 Application.Quit 方法加以關閉。

C#
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

備註

GetTypeFromCLSID當您知道 COM 物件的類別識別碼 (CLSID) 時,此方法支援從.NET Framework應用程式對 Unmanaged COM 物件的晚期繫結存取。 COM 類別的類別識別碼定義于登錄HKEY_CLASSES_ROOT\CLSID機碼中。 您可以擷取 屬性的值,以判斷這個方法傳回的類型 IsCOMObject 是否為 COM 物件。

提示

您可以呼叫 GetTypeFromProgID 方法,以取得 COM 物件的晚期繫結存取,其程式設計識別碼 (ProgID) 您知道。

從 CLSID 具現化 Unmanaged COM 物件是兩個步驟的程式:

  1. Type取得 物件,這個物件表示 __ComObject 透過呼叫 方法對應至 CLSID 的 GetTypeFromCLSID

  2. Activator.CreateInstance(Type)呼叫 方法以具現化 COM 物件。

如需圖例,請參閱範例。

GetTypeFromCLSID(Guid) 載會忽略根據 clsid 引數具現化 Type 物件時可能發生的任何例外狀況。 請注意,如果在 clsid 登錄中找不到,則不會擲回例外狀況。

給呼叫者的注意事項

這個方法適用于使用 COM 物件,而不是搭配 .NET Framework 物件使用。 所有 Managed 物件,包括 COM (可見的物件,也就是其 ComVisibleAttribute 屬性 true) 具有 屬性所傳回的 GUID GUID。 雖然 方法會傳 Type 回對應至 .NET Framework 物件的 GUID 的物件,但您無法透過 Type 呼叫 CreateInstance(Type) 方法使用該物件來建立類型實例,如下列範例所示。

C#
using System;
using System.Runtime.InteropServices;

[assembly:ComVisible(true)]

// Define two classes, and assign one an explicit GUID.
[GuidAttribute("d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4")]
public class ExplicitGuid
{ }

public class NoExplicitGuid
{ }

public class Example
{
   public static void Main()
   {
      Type explicitType = typeof(ExplicitGuid);
      Guid explicitGuid = explicitType.GUID;
      
      // Get type of ExplicitGuid from its GUID.
      Type explicitCOM = Type.GetTypeFromCLSID(explicitGuid);
      Console.WriteLine("Created {0} type from CLSID {1}",
                        explicitCOM.Name, explicitGuid);
                        
      // Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        explicitType.Name, explicitCOM.Name,
                        explicitType.Equals(explicitCOM));                  
      
      // Instantiate an ExplicitGuid object.
      try {
         Object obj = Activator.CreateInstance(explicitCOM);
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name);
      } 
      catch (COMException e) {
         Console.WriteLine("COM Exception:\n{0}\n", e.Message);   
      } 
        
      Type notExplicit = typeof(NoExplicitGuid);
      Guid notExplicitGuid = notExplicit.GUID;
      
      // Get type of ExplicitGuid from its GUID.
      Type notExplicitCOM = Type.GetTypeFromCLSID(notExplicitGuid);
      Console.WriteLine("Created {0} type from CLSID {1}",
                        notExplicitCOM.Name, notExplicitGuid);
                        
      // Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        notExplicit.Name, notExplicitCOM.Name,
                        notExplicit.Equals(notExplicitCOM));                  
      
      // Instantiate an ExplicitGuid object.
      try {
         Object obj = Activator.CreateInstance(notExplicitCOM);
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name);
      } 
      catch (COMException e) {
         Console.WriteLine("COM Exception:\n{0}\n", e.Message);   
      }   
   }
}
// The example displays the following output:
//       Created __ComObject type from CLSID d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4
//       ExplicitGuid and __ComObject equal: False
//       COM Exception:
//       Retrieving the COM class factory for component with CLSID 
//       {D055CBA3-1F83-4BD7-BA19-E22B1B8EC3C4} failed due to the following error: 
//       80040154 Class not registered 
//       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
//       
//       Created __ComObject type from CLSID 74f03346-a718-3516-ac78-f351c7459ffb
//       NoExplicitGuid and __ComObject equal: False
//       COM Exception:
//       Retrieving the COM class factory for component with CLSID 
//       {74F03346-A718-3516-AC78-F351C7459FFB} failed due to the following error: 
//       80040154 Class not registered 
//       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

相反地, GetTypeFromCLSID(Guid, String, Boolean) 應該只用來擷取 Unmanaged COM 物件的 GUID,而傳遞給 CreateInstance(Type) 方法的結果 Type 物件必須代表 Unmanaged COM 物件。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetTypeFromCLSID(Guid, Boolean)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

取得與指定的類別識別項 (CLSID) 關聯的類型,並指定如果載入類型時發生錯誤是否擲回例外狀況。

C#
public static Type? GetTypeFromCLSID (Guid clsid, bool throwOnError);
C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID (Guid clsid, bool throwOnError);
C#
public static Type GetTypeFromCLSID (Guid clsid, bool throwOnError);

參數

clsid
Guid

要取得之類型的 CLSID。

throwOnError
Boolean

true 則擲回任何會發生的例外狀況。

-或-

false 則忽略任何會發生的例外狀況。

傳回

System.__ComObject (不論 CLSID 是否有效)。

屬性

範例

下列範例會使用 Microsoft Word Application 物件的 CLSID 來擷取代表 Microsoft Word 應用程式的 COM 類型。 然後它會呼叫 Activator.CreateInstance 方法來具現化類型,並藉由呼叫 Application.Quit 方法加以關閉。 如果載入類型時發生錯誤,就會擲回例外狀況。

C#
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

備註

GetTypeFromCLSID當您知道 COM 物件的類別識別碼 (CLSID) 時,此方法支援從.NET Framework應用程式對 Unmanaged COM 物件的晚期繫結存取。 COM 類別的類別識別碼定義于登錄HKEY_CLASSES_ROOT\CLSID機碼中。 您可以擷取 屬性的值,以判斷這個方法傳回的類型 IsCOMObject 是否為 COM 物件。

提示

您可以呼叫 GetTypeFromProgID 方法,以取得 COM 物件的晚期繫結存取,其程式設計識別碼 (ProgID) 您知道。

從 CLSID 具現化 Unmanaged COM 物件是兩個步驟的程式:

  1. Type取得 物件,這個物件表示 __ComObject 透過呼叫 方法對應至 CLSID 的 GetTypeFromCLSID

  2. Activator.CreateInstance(Type)呼叫 方法以具現化 COM 物件。

如需圖例,請參閱範例。

為 指定 truethrowOnError 時會擲回之類的 OutOfMemoryException 例外狀況,但未註冊的 CLSID 不會失敗。

給呼叫者的注意事項

這個方法適用于使用 COM 物件,而不是搭配 .NET Framework 物件使用。 所有 Managed 物件,包括 COM (可見的物件,也就是其 ComVisibleAttribute 屬性 true) 具有 屬性所傳回的 GUID GUID。 雖然 方法會傳 Type 回對應至 .NET Framework 物件的 GUID 的物件,但您無法透過 Type 呼叫 CreateInstance(Type) 方法使用該物件來建立類型實例,如下列範例所示。

C#
using System;
using System.Runtime.InteropServices;

[assembly:ComVisible(true)]

// Define two classes, and assign one an explicit GUID.
[GuidAttribute("d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4")]
public class ExplicitGuid
{ }

public class NoExplicitGuid
{ }

public class Example
{
   public static void Main()
   {
      Type explicitType = typeof(ExplicitGuid);
      Guid explicitGuid = explicitType.GUID;
      
      // Get type of ExplicitGuid from its GUID.
      Type explicitCOM = Type.GetTypeFromCLSID(explicitGuid);
      Console.WriteLine("Created {0} type from CLSID {1}",
                        explicitCOM.Name, explicitGuid);
                        
      // Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        explicitType.Name, explicitCOM.Name,
                        explicitType.Equals(explicitCOM));                  
      
      // Instantiate an ExplicitGuid object.
      try {
         Object obj = Activator.CreateInstance(explicitCOM);
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name);
      } 
      catch (COMException e) {
         Console.WriteLine("COM Exception:\n{0}\n", e.Message);   
      } 
        
      Type notExplicit = typeof(NoExplicitGuid);
      Guid notExplicitGuid = notExplicit.GUID;
      
      // Get type of ExplicitGuid from its GUID.
      Type notExplicitCOM = Type.GetTypeFromCLSID(notExplicitGuid);
      Console.WriteLine("Created {0} type from CLSID {1}",
                        notExplicitCOM.Name, notExplicitGuid);
                        
      // Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        notExplicit.Name, notExplicitCOM.Name,
                        notExplicit.Equals(notExplicitCOM));                  
      
      // Instantiate an ExplicitGuid object.
      try {
         Object obj = Activator.CreateInstance(notExplicitCOM);
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name);
      } 
      catch (COMException e) {
         Console.WriteLine("COM Exception:\n{0}\n", e.Message);   
      }   
   }
}
// The example displays the following output:
//       Created __ComObject type from CLSID d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4
//       ExplicitGuid and __ComObject equal: False
//       COM Exception:
//       Retrieving the COM class factory for component with CLSID 
//       {D055CBA3-1F83-4BD7-BA19-E22B1B8EC3C4} failed due to the following error: 
//       80040154 Class not registered 
//       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
//       
//       Created __ComObject type from CLSID 74f03346-a718-3516-ac78-f351c7459ffb
//       NoExplicitGuid and __ComObject equal: False
//       COM Exception:
//       Retrieving the COM class factory for component with CLSID 
//       {74F03346-A718-3516-AC78-F351C7459FFB} failed due to the following error: 
//       80040154 Class not registered 
//       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

相反地, GetTypeFromCLSID(Guid, String, Boolean) 應該只用來擷取 Unmanaged COM 物件的 GUID,而傳遞給 CreateInstance(Type) 方法的結果 Type 物件必須代表 Unmanaged COM 物件。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetTypeFromCLSID(Guid, String)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

從指定的伺服器中,取得與指定的類別識別項 (CLSID) 相關聯的類型。

C#
public static Type? GetTypeFromCLSID (Guid clsid, string? server);
C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID (Guid clsid, string? server);
C#
public static Type GetTypeFromCLSID (Guid clsid, string server);

參數

clsid
Guid

要取得之類型的 CLSID。

server
String

要載入類型的伺服器。 如果伺服器名稱為 null,此方法將會自動還原成本機電腦 (Local Machine)。

傳回

System.__ComObject (不論 CLSID 是否有效)。

屬性

範例

下列範例會使用 Microsoft Word Application 物件的 CLSID,從名為 computer17.central.contoso.com 的伺服器擷取代表 Microsoft Word 應用程式的 COM 類型。 然後它會呼叫 Activator.CreateInstance 方法來具現化類型,並藉由呼叫 Application.Quit 方法加以關閉。

C#
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

備註

GetTypeFromCLSID當您知道 COM 物件的類別識別碼 (CLSID) 時,此方法支援從.NET Framework應用程式對 Unmanaged COM 物件的晚期繫結存取。 COM 類別的類別識別碼定義于登錄HKEY_CLASSES_ROOT\CLSID機碼中。 您可以擷取 屬性的值,以判斷這個方法傳回的類型 IsCOMObject 是否為 COM 物件。

提示

您可以呼叫 GetTypeFromProgID 方法,以取得 COM 物件的晚期繫結存取,其程式設計識別碼 (ProgID) 您知道。

從 CLSID 具現化 Unmanaged COM 物件是兩個步驟的程式:

  1. Type取得 物件,這個物件表示 __ComObject 透過呼叫 方法對應至 CLSID 的 GetTypeFromCLSID

  2. Activator.CreateInstance(Type)呼叫 方法以具現化 COM 物件。

給呼叫者的注意事項

這個方法適用于使用 COM 物件,而不是搭配 .NET Framework 物件使用。 所有 Managed 物件,包括 COM (可見的物件,也就是其 ComVisibleAttribute 屬性 true) 具有 屬性所傳回的 GUID GUID。 雖然 方法會傳 Type 回對應至 .NET Framework 物件的 GUID 的物件,但您無法透過 Type 呼叫 CreateInstance(Type) 方法使用該物件來建立類型實例,如下列範例所示。

C#
using System;
using System.Runtime.InteropServices;

[assembly:ComVisible(true)]

// Define two classes, and assign one an explicit GUID.
[GuidAttribute("d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4")]
public class ExplicitGuid
{ }

public class NoExplicitGuid
{ }

public class Example
{
   public static void Main()
   {
      Type explicitType = typeof(ExplicitGuid);
      Guid explicitGuid = explicitType.GUID;
      
      // Get type of ExplicitGuid from its GUID.
      Type explicitCOM = Type.GetTypeFromCLSID(explicitGuid);
      Console.WriteLine("Created {0} type from CLSID {1}",
                        explicitCOM.Name, explicitGuid);
                        
      // Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        explicitType.Name, explicitCOM.Name,
                        explicitType.Equals(explicitCOM));                  
      
      // Instantiate an ExplicitGuid object.
      try {
         Object obj = Activator.CreateInstance(explicitCOM);
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name);
      } 
      catch (COMException e) {
         Console.WriteLine("COM Exception:\n{0}\n", e.Message);   
      } 
        
      Type notExplicit = typeof(NoExplicitGuid);
      Guid notExplicitGuid = notExplicit.GUID;
      
      // Get type of ExplicitGuid from its GUID.
      Type notExplicitCOM = Type.GetTypeFromCLSID(notExplicitGuid);
      Console.WriteLine("Created {0} type from CLSID {1}",
                        notExplicitCOM.Name, notExplicitGuid);
                        
      // Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        notExplicit.Name, notExplicitCOM.Name,
                        notExplicit.Equals(notExplicitCOM));                  
      
      // Instantiate an ExplicitGuid object.
      try {
         Object obj = Activator.CreateInstance(notExplicitCOM);
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name);
      } 
      catch (COMException e) {
         Console.WriteLine("COM Exception:\n{0}\n", e.Message);   
      }   
   }
}
// The example displays the following output:
//       Created __ComObject type from CLSID d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4
//       ExplicitGuid and __ComObject equal: False
//       COM Exception:
//       Retrieving the COM class factory for component with CLSID 
//       {D055CBA3-1F83-4BD7-BA19-E22B1B8EC3C4} failed due to the following error: 
//       80040154 Class not registered 
//       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
//       
//       Created __ComObject type from CLSID 74f03346-a718-3516-ac78-f351c7459ffb
//       NoExplicitGuid and __ComObject equal: False
//       COM Exception:
//       Retrieving the COM class factory for component with CLSID 
//       {74F03346-A718-3516-AC78-F351C7459FFB} failed due to the following error: 
//       80040154 Class not registered 
//       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

相反地, GetTypeFromCLSID(Guid, String, Boolean) 應該只用來擷取 Unmanaged COM 物件的 GUID,而傳遞給 CreateInstance(Type) 方法的結果 Type 物件必須代表 Unmanaged COM 物件。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

GetTypeFromCLSID(Guid, String, Boolean)

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

從指定的伺服器中,取得與指定的類別識別項 (CLSID) 相關聯的類型,並指定如果在載入類型時發生錯誤是否擲回例外狀況。

C#
public static Type? GetTypeFromCLSID (Guid clsid, string? server, bool throwOnError);
C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static Type? GetTypeFromCLSID (Guid clsid, string? server, bool throwOnError);
C#
public static Type GetTypeFromCLSID (Guid clsid, string server, bool throwOnError);

參數

clsid
Guid

要取得之類型的 CLSID。

server
String

要載入類型的伺服器。 如果伺服器名稱為 null,此方法將會自動還原成本機電腦 (Local Machine)。

throwOnError
Boolean

true 則擲回任何會發生的例外狀況。

-或-

false 則忽略任何會發生的例外狀況。

傳回

System.__ComObject (不論 CLSID 是否有效)。

屬性

範例

下列範例會使用 Microsoft Word Application 物件的 CLSID,從名為 computer17.central.contoso.com 的伺服器擷取代表 Microsoft Word 應用程式的 COM 類型。 然後它會呼叫 Activator.CreateInstance 方法來具現化類型,並藉由呼叫 Application.Quit 方法加以關閉。 如果載入類型時發生錯誤,就會擲回例外狀況。

C#
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

備註

GetTypeFromCLSID當您知道 COM 物件的類別識別碼 (CLSID) 時,此方法支援從.NET Framework應用程式對 Unmanaged COM 物件的晚期繫結存取。 COM 類別的類別識別碼定義于登錄HKEY_CLASSES_ROOT\CLSID機碼中。 您可以擷取 屬性的值,以判斷這個方法傳回的類型 IsCOMObject 是否為 COM 物件。

提示

您可以呼叫 GetTypeFromProgID 方法,以取得 COM 物件的晚期繫結存取,其程式設計識別碼 (ProgID) 您知道。

從 CLSID 具現化 Unmanaged COM 物件是兩個步驟的程式:

  1. Type取得 物件,這個物件表示 __ComObject 透過呼叫 方法對應至 CLSID 的 GetTypeFromCLSID

  2. Activator.CreateInstance(Type)呼叫 方法以具現化 COM 物件。

為 指定 truethrowOnError 時會擲回之類的 OutOfMemoryException 例外狀況,但未註冊的 CLSID 不會失敗。

給呼叫者的注意事項

這個方法適用于使用 COM 物件,而不是搭配 .NET Framework 物件使用。 所有 Managed 物件,包括 COM (可見的物件,也就是其 ComVisibleAttribute 屬性 true) 具有 屬性所傳回的 GUID GUID。 雖然 方法會 GetTypeFromCLSID(Guid, String, Boolean)Type 回對應至特定 Managed 物件的 GUID 的物件,但您無法透過 Type 呼叫 CreateInstance(Type) 方法使用該物件來建立類型實例,如下列範例所示。

C#
using System;
using System.Runtime.InteropServices;

[assembly:ComVisible(true)]

// Define two classes, and assign one an explicit GUID.
[GuidAttribute("d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4")]
public class ExplicitGuid
{ }

public class NoExplicitGuid
{ }

public class Example
{
   public static void Main()
   {
      Type explicitType = typeof(ExplicitGuid);
      Guid explicitGuid = explicitType.GUID;
      
      // Get type of ExplicitGuid from its GUID.
      Type explicitCOM = Type.GetTypeFromCLSID(explicitGuid);
      Console.WriteLine("Created {0} type from CLSID {1}",
                        explicitCOM.Name, explicitGuid);
                        
      // Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        explicitType.Name, explicitCOM.Name,
                        explicitType.Equals(explicitCOM));                  
      
      // Instantiate an ExplicitGuid object.
      try {
         Object obj = Activator.CreateInstance(explicitCOM);
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name);
      } 
      catch (COMException e) {
         Console.WriteLine("COM Exception:\n{0}\n", e.Message);   
      } 
        
      Type notExplicit = typeof(NoExplicitGuid);
      Guid notExplicitGuid = notExplicit.GUID;
      
      // Get type of ExplicitGuid from its GUID.
      Type notExplicitCOM = Type.GetTypeFromCLSID(notExplicitGuid);
      Console.WriteLine("Created {0} type from CLSID {1}",
                        notExplicitCOM.Name, notExplicitGuid);
                        
      // Compare the two type objects.
      Console.WriteLine("{0} and {1} equal: {2}",
                        notExplicit.Name, notExplicitCOM.Name,
                        notExplicit.Equals(notExplicitCOM));                  
      
      // Instantiate an ExplicitGuid object.
      try {
         Object obj = Activator.CreateInstance(notExplicitCOM);
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name);
      } 
      catch (COMException e) {
         Console.WriteLine("COM Exception:\n{0}\n", e.Message);   
      }   
   }
}
// The example displays the following output:
//       Created __ComObject type from CLSID d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4
//       ExplicitGuid and __ComObject equal: False
//       COM Exception:
//       Retrieving the COM class factory for component with CLSID 
//       {D055CBA3-1F83-4BD7-BA19-E22B1B8EC3C4} failed due to the following error: 
//       80040154 Class not registered 
//       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
//       
//       Created __ComObject type from CLSID 74f03346-a718-3516-ac78-f351c7459ffb
//       NoExplicitGuid and __ComObject equal: False
//       COM Exception:
//       Retrieving the COM class factory for component with CLSID 
//       {74F03346-A718-3516-AC78-F351C7459FFB} failed due to the following error: 
//       80040154 Class not registered 
//       (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

相反地, GetTypeFromCLSID(Guid, String, Boolean) 應該只用來擷取 Unmanaged COM 物件的 GUID,而傳遞給 CreateInstance(Type) 方法的結果 Type 物件必須代表 Unmanaged COM 物件。

適用於

.NET 9 和其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1