Type.GetTypeFromCLSID メソッド

定義

指定したクラス識別子 (CLSID) に関連付けられている型を取得します。

オーバーロード

GetTypeFromCLSID(Guid)

指定したクラス識別子 (CLSID) に関連付けられている型を取得します。

GetTypeFromCLSID(Guid, Boolean)

型の読み込み中にエラーが発生した場合に例外をスローするかどうかを指定して、指定したクラス ID (CLSID) に関連付けられた型を取得します。

GetTypeFromCLSID(Guid, String)

指定したサーバーから、指定したクラス ID (CLSID) に関連付けられている型を取得します。

GetTypeFromCLSID(Guid, String, Boolean)

型の読み込み中にエラーが発生した場合に例外をスローするかどうかを指定して、指定したサーバーの指定したクラス ID (CLSID) に関連付けられた型を取得します。

GetTypeFromCLSID(Guid)

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

指定したクラス識別子 (CLSID) に関連付けられている型を取得します。

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

パラメーター

clsid
Guid

取得する型の CLSID。

戻り値

CLSID が有効かどうかに関係なく System.__ComObject

属性

次の例では、Microsoft Word Application オブジェクト の CLSID を使用して、Microsoft Word アプリケーションを表す COM 型を取得します。 次に、 メソッドを呼び出して型を Activator.CreateInstance インスタンス化し、 Application.Quit メソッドを呼び出して型を閉じます。

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

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

let [<Literal>] WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}"
   
// Start an instance of the Word application.
let word = Type.GetTypeFromCLSID(Guid.Parse WORD_CLSID)
printfn $"Instantiated Type object from CLSID {WORD_CLSID}"
let wordObj = Activator.CreateInstance word
printfn $"Instantiated {wordObj.GetType().FullName}"

// Close Word.
word.InvokeMember("Quit", BindingFlags.InvokeMethod, null, wordObj, [| box 0; 0; false |]) |> ignore
// 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 オブジェクトへの遅延バインディング アクセスをサポートします。 COM クラスのクラス識別子は、レジストリのHKEY_CLASSES_ROOT\CLSID キーで定義されます。 プロパティの値を取得して、 IsCOMObject このメソッドによって返される型が COM オブジェクトであるかどうかを判断できます。

ヒント

プログラム識別子 (ProgID) がわかっている COM オブジェクトへの遅延バインディング アクセスのメソッドを呼び出 GetTypeFromProgID すことができます。

CLSID からアンマネージ COM オブジェクトをインスタンス化することは、次の 2 段階のプロセスです。

  1. メソッドを Type 呼び出して、CLSID に対応する を表す__ComObject オブジェクトを GetTypeFromCLSID 取得します。

  2. メソッドを Activator.CreateInstance(Type) 呼び出して COM オブジェクトをインスタンス化します。

図については、例を参照してください。

オーバーロードは GetTypeFromCLSID(Guid) 、 引数に基づいてオブジェクトをインスタンス化するときに発生する Type 可能性のある例外を clsid 無視します。 がレジストリで見つからない場合 clsid 、例外はスローされないことに注意してください。

注意 (呼び出し元)

このメソッドは、.NET Framework オブジェクトではなく COM オブジェクトを操作する場合に使用することを目的としています。 COM に表示されるオブジェクト (つまり、属性 ComVisibleAttribute が ) を含むすべてのマネージド オブジェクトには true、 プロパティによって GUID 返される GUID があります。 メソッドは、.NET Framework オブジェクトの GUID に対応するオブジェクトを返Typeしますが、次の例に示すように、 メソッドを呼び出して、そのTypeオブジェクトをCreateInstance(Type)使用して型インスタンスを作成することはできません。

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)).
open System
open System.Runtime.InteropServices

[<assembly: ComVisible true>]
do ()

// Define two classes, and assign one an explicit GUID.
[<Guid "d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4">]
type ExplicitGuid() = class end

type NoExplicitGuid() = class end

let explicitType = typeof<ExplicitGuid>
let explicitGuid = explicitType.GUID

// Get type of ExplicitGuid from its GUID.
let explicitCOM = Type.GetTypeFromCLSID explicitGuid
printfn $"Created {explicitCOM.Name} type from CLSID {explicitGuid}"
                
// Compare the two type objects.
printfn $"{explicitType.Name} and {explicitCOM.Name} equal: {explicitType.Equals explicitCOM}"       

// Instantiate an ExplicitGuid object.
try
    let obj = Activator.CreateInstance explicitCOM
    printfn $"Instantiated a {obj.GetType().Name} object"
with :? COMException as e ->
    printfn $"COM Exception:\n{e.Message}\n"

let notExplicit = typeof<NoExplicitGuid>
let notExplicitGuid = notExplicit.GUID

// Get type of ExplicitGuid from its GUID.
let notExplicitCOM = Type.GetTypeFromCLSID(notExplicitGuid)
printfn $"Created {notExplicitCOM.Name} type from CLSID {notExplicitGuid}"
                
// Compare the two type objects.
printfn $"{notExplicit.Name} and {notExplicitCOM.Name} equal: {notExplicit.Equals notExplicitCOM}"       

// Instantiate an ExplicitGuid object.
try
    let obj = Activator.CreateInstance notExplicitCOM
    printfn $"Instantiated a {obj.GetType().Name} object"
with :? COMException as e ->
    printfn $"COM Exception:\n{e.Message}\n"   
// 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)).
Imports System.Runtime.InteropServices

<Assembly:ComVisible(True)>

' Define two classes, and assign one an explicit GUID.
<GuidAttribute("d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4")>
Public Class ExplicitGuid
End Class

Public Class NoExplicitGuid
End Class

Module Example
   Public Sub Main()
      Dim explicitType As Type = GetType(ExplicitGuid)
      Dim explicitGuid As Guid = explicitType.GUID
      
      ' Get type of ExplicitGuid from its GUID.
      Dim explicitCOM As Type = 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 
         Dim obj As Object = Activator.CreateInstance(explicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try 
        
      Dim notExplicit As Type = GetType(NoExplicitGuid)
      Dim notExplicitGuid As Guid = notExplicit.GUID
      
      ' Get type of ExplicitGuid from its GUID.
      Dim notExplicitCOM As Type = 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 
         Dim obj As Object = Activator.CreateInstance(notExplicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try 
   End Sub
End Module
' 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)使用してアンマネージド COM オブジェクトの GUID を取得する必要があります。メソッドにCreateInstance(Type)渡される結果Typeのオブジェクトは、アンマネージ COM オブジェクトを表す必要があります。

適用対象

GetTypeFromCLSID(Guid, Boolean)

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

型の読み込み中にエラーが発生した場合に例外をスローするかどうかを指定して、指定したクラス ID (CLSID) に関連付けられた型を取得します。

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

パラメーター

clsid
Guid

取得する型の CLSID。

throwOnError
Boolean

発生した例外をすべてスローする場合は true

または

発生した例外をすべて無視する場合は false

戻り値

CLSID が有効かどうかに関係なく System.__ComObject

属性

次の例では、Microsoft Word Application オブジェクト の CLSID を使用して、Microsoft Word アプリケーションを表す COM 型を取得します。 次に、 メソッドを呼び出して型を Activator.CreateInstance インスタンス化し、 Application.Quit メソッドを呼び出して型を閉じます。 型の読み込み中にエラーが発生すると、例外がスローされます。

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

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

let [<Literal>] WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}"

try
    // Start an instance of the Word application.
    let word = Type.GetTypeFromCLSID(Guid.Parse WORD_CLSID, true)
    printfn $"Instantiated Type object from CLSID {WORD_CLSID}"
    let wordObj = Activator.CreateInstance word
    printfn $"Instantiated {wordObj.GetType().FullName} from CLSID {WORD_CLSID}" 
    
    // Close Word.
    word.InvokeMember("Quit", BindingFlags.InvokeMethod, null, wordObj, [| box 0; 0; false |] ) |> ignore
with _ ->
    printfn $"Unable to instantiate an object for {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 オブジェクトへの遅延バインディング アクセスをサポートします。 COM クラスのクラス識別子は、レジストリのHKEY_CLASSES_ROOT\CLSID キーで定義されます。 プロパティの値を取得して、 IsCOMObject このメソッドによって返される型が COM オブジェクトであるかどうかを判断できます。

ヒント

プログラム識別子 (ProgID) がわかっている COM オブジェクトへの遅延バインディング アクセスのメソッドを呼び出 GetTypeFromProgID すことができます。

CLSID からアンマネージ COM オブジェクトをインスタンス化することは、次の 2 段階のプロセスです。

  1. メソッドを Type 呼び出して、CLSID に対応する を表す __ComObject オブジェクトを GetTypeFromCLSID 取得します。

  2. メソッドを Activator.CreateInstance(Type) 呼び出して COM オブジェクトをインスタンス化します。

図については、例を参照してください。

などのOutOfMemoryException例外は、 にをtruethrowOnError指定するときにスローされますが、登録されていない CLSID では失敗しません。

注意 (呼び出し元)

このメソッドは、.NET Framework オブジェクトではなく COM オブジェクトを操作する場合に使用することを目的としています。 COM に表示されるオブジェクト (つまり、属性 ComVisibleAttribute が ) を含むすべてのマネージド オブジェクトには true、 プロパティによって GUID 返される GUID があります。 メソッドは、.NET Framework オブジェクトの GUID に対応するオブジェクトを返Typeしますが、次の例に示すように、 メソッドを呼び出して、そのTypeオブジェクトをCreateInstance(Type)使用して型インスタンスを作成することはできません。

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)).
open System
open System.Runtime.InteropServices

[<assembly: ComVisible true>]
do ()

// Define two classes, and assign one an explicit GUID.
[<Guid "d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4">]
type ExplicitGuid() = class end

type NoExplicitGuid() = class end

let explicitType = typeof<ExplicitGuid>
let explicitGuid = explicitType.GUID

// Get type of ExplicitGuid from its GUID.
let explicitCOM = Type.GetTypeFromCLSID explicitGuid
printfn $"Created {explicitCOM.Name} type from CLSID {explicitGuid}"
                
// Compare the two type objects.
printfn $"{explicitType.Name} and {explicitCOM.Name} equal: {explicitType.Equals explicitCOM}"       

// Instantiate an ExplicitGuid object.
try
    let obj = Activator.CreateInstance explicitCOM
    printfn $"Instantiated a {obj.GetType().Name} object"
with :? COMException as e ->
    printfn $"COM Exception:\n{e.Message}\n"

let notExplicit = typeof<NoExplicitGuid>
let notExplicitGuid = notExplicit.GUID

// Get type of ExplicitGuid from its GUID.
let notExplicitCOM = Type.GetTypeFromCLSID(notExplicitGuid)
printfn $"Created {notExplicitCOM.Name} type from CLSID {notExplicitGuid}"
                
// Compare the two type objects.
printfn $"{notExplicit.Name} and {notExplicitCOM.Name} equal: {notExplicit.Equals notExplicitCOM}"       

// Instantiate an ExplicitGuid object.
try
    let obj = Activator.CreateInstance notExplicitCOM
    printfn $"Instantiated a {obj.GetType().Name} object"
with :? COMException as e ->
    printfn $"COM Exception:\n{e.Message}\n"   
// 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)).
Imports System.Runtime.InteropServices

<Assembly:ComVisible(True)>

' Define two classes, and assign one an explicit GUID.
<GuidAttribute("d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4")>
Public Class ExplicitGuid
End Class

Public Class NoExplicitGuid
End Class

Module Example
   Public Sub Main()
      Dim explicitType As Type = GetType(ExplicitGuid)
      Dim explicitGuid As Guid = explicitType.GUID
      
      ' Get type of ExplicitGuid from its GUID.
      Dim explicitCOM As Type = 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 
         Dim obj As Object = Activator.CreateInstance(explicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try 
        
      Dim notExplicit As Type = GetType(NoExplicitGuid)
      Dim notExplicitGuid As Guid = notExplicit.GUID
      
      ' Get type of ExplicitGuid from its GUID.
      Dim notExplicitCOM As Type = 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 
         Dim obj As Object = Activator.CreateInstance(notExplicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try 
   End Sub
End Module
' 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)使用してアンマネージド COM オブジェクトの GUID を取得する必要があります。メソッドにCreateInstance(Type)渡される結果Typeのオブジェクトは、アンマネージ COM オブジェクトを表す必要があります。

適用対象

GetTypeFromCLSID(Guid, String)

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

指定したサーバーから、指定したクラス ID (CLSID) に関連付けられている型を取得します。

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

パラメーター

clsid
Guid

取得する型の CLSID。

server
String

型の読み込み元のサーバー。 サーバー名が nullの場合、このメソッドは自動的にローカル マシンの名前を使用します。

戻り値

CLSID が有効かどうかに関係なく System.__ComObject

属性

次の例では、Microsoft Word Application オブジェクト の CLSID を使用して、computer17.central.contoso.com という名前のサーバーから Microsoft Word アプリケーションを表す COM 型を取得します。 次に、 メソッドを呼び出して型を Activator.CreateInstance インスタンス化し、 Application.Quit メソッドを呼び出して型を閉じます。

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

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

let [<Literal>] WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}"

// Start an instance of the Word application.
let word = Type.GetTypeFromCLSID(Guid.Parse WORD_CLSID, "computer17.central.contoso.com")
printfn $"Instantiated Type object from CLSID {WORD_CLSID}"
try
    let wordObj = Activator.CreateInstance word
    printfn $"Instantiated {wordObj.GetType().FullName} from CLSID {WORD_CLSID}" 
    
    // Close Word.
    word.InvokeMember("Quit", BindingFlags.InvokeMethod, null, wordObj, [| box 0; 0; false |] ) |> ignore
with :? COMException ->
    printfn "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 オブジェクトへの遅延バインディング アクセスをサポートします。 COM クラスのクラス識別子は、レジストリのHKEY_CLASSES_ROOT\CLSID キーで定義されます。 プロパティの値を取得して、 IsCOMObject このメソッドによって返される型が COM オブジェクトであるかどうかを判断できます。

ヒント

プログラム識別子 (ProgID) がわかっている COM オブジェクトへの遅延バインディング アクセスのメソッドを呼び出 GetTypeFromProgID すことができます。

CLSID からアンマネージ COM オブジェクトをインスタンス化することは、次の 2 段階のプロセスです。

  1. メソッドを Type 呼び出して、CLSID に対応する を表す __ComObject オブジェクトを GetTypeFromCLSID 取得します。

  2. メソッドを Activator.CreateInstance(Type) 呼び出して COM オブジェクトをインスタンス化します。

注意 (呼び出し元)

このメソッドは、.NET Framework オブジェクトではなく COM オブジェクトを操作する場合に使用することを目的としています。 COM に表示されるオブジェクト (つまり、属性 ComVisibleAttribute が ) を含むすべてのマネージド オブジェクトには true、 プロパティによって GUID 返される GUID があります。 メソッドは、.NET Framework オブジェクトの GUID に対応するオブジェクトを返Typeしますが、次の例に示すように、 メソッドを呼び出して、そのTypeオブジェクトをCreateInstance(Type)使用して型インスタンスを作成することはできません。

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)).
open System
open System.Runtime.InteropServices

[<assembly: ComVisible true>]
do ()

// Define two classes, and assign one an explicit GUID.
[<Guid "d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4">]
type ExplicitGuid() = class end

type NoExplicitGuid() = class end

let explicitType = typeof<ExplicitGuid>
let explicitGuid = explicitType.GUID

// Get type of ExplicitGuid from its GUID.
let explicitCOM = Type.GetTypeFromCLSID explicitGuid
printfn $"Created {explicitCOM.Name} type from CLSID {explicitGuid}"
                
// Compare the two type objects.
printfn $"{explicitType.Name} and {explicitCOM.Name} equal: {explicitType.Equals explicitCOM}"       

// Instantiate an ExplicitGuid object.
try
    let obj = Activator.CreateInstance explicitCOM
    printfn $"Instantiated a {obj.GetType().Name} object"
with :? COMException as e ->
    printfn $"COM Exception:\n{e.Message}\n"

let notExplicit = typeof<NoExplicitGuid>
let notExplicitGuid = notExplicit.GUID

// Get type of ExplicitGuid from its GUID.
let notExplicitCOM = Type.GetTypeFromCLSID(notExplicitGuid)
printfn $"Created {notExplicitCOM.Name} type from CLSID {notExplicitGuid}"
                
// Compare the two type objects.
printfn $"{notExplicit.Name} and {notExplicitCOM.Name} equal: {notExplicit.Equals notExplicitCOM}"       

// Instantiate an ExplicitGuid object.
try
    let obj = Activator.CreateInstance notExplicitCOM
    printfn $"Instantiated a {obj.GetType().Name} object"
with :? COMException as e ->
    printfn $"COM Exception:\n{e.Message}\n"   
// 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)).
Imports System.Runtime.InteropServices

<Assembly:ComVisible(True)>

' Define two classes, and assign one an explicit GUID.
<GuidAttribute("d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4")>
Public Class ExplicitGuid
End Class

Public Class NoExplicitGuid
End Class

Module Example
   Public Sub Main()
      Dim explicitType As Type = GetType(ExplicitGuid)
      Dim explicitGuid As Guid = explicitType.GUID
      
      ' Get type of ExplicitGuid from its GUID.
      Dim explicitCOM As Type = 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 
         Dim obj As Object = Activator.CreateInstance(explicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try 
        
      Dim notExplicit As Type = GetType(NoExplicitGuid)
      Dim notExplicitGuid As Guid = notExplicit.GUID
      
      ' Get type of ExplicitGuid from its GUID.
      Dim notExplicitCOM As Type = 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 
         Dim obj As Object = Activator.CreateInstance(notExplicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try 
   End Sub
End Module
' 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)使用してアンマネージド COM オブジェクトの GUID を取得する必要があります。メソッドにCreateInstance(Type)渡される結果Typeのオブジェクトは、アンマネージ COM オブジェクトを表す必要があります。

適用対象

GetTypeFromCLSID(Guid, String, Boolean)

ソース:
Type.cs
ソース:
Type.cs
ソース:
Type.cs

型の読み込み中にエラーが発生した場合に例外をスローするかどうかを指定して、指定したサーバーの指定したクラス ID (CLSID) に関連付けられた型を取得します。

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

パラメーター

clsid
Guid

取得する型の CLSID。

server
String

型の読み込み元のサーバー。 サーバー名が nullの場合、このメソッドは自動的にローカル マシンの名前を使用します。

throwOnError
Boolean

発生した例外をすべてスローする場合は true

または

発生した例外をすべて無視する場合は false

戻り値

CLSID が有効かどうかに関係なく System.__ComObject

属性

次の例では、Microsoft Word Application オブジェクト の CLSID を使用して、computer17.central.contoso.com という名前のサーバーから Microsoft Word アプリケーションを表す COM 型を取得します。 次に、 メソッドを呼び出して型を Activator.CreateInstance インスタンス化し、 Application.Quit メソッドを呼び出して型を閉じます。 型の読み込み中にエラーが発生すると、例外がスローされます。

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

public class Example
{
   private const string WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}";
   
   public static void Main()
   {
      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
open System
open System.Reflection

let [<Literal>] WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}"

try
    // Start an instance of the Word application.
    let word = Type.GetTypeFromCLSID(Guid.Parse WORD_CLSID, "computer17.central.contoso.com", true)
    printfn $"Instantiated Type object from CLSID {WORD_CLSID}"
    let wordObj = Activator.CreateInstance word
    printfn $"Instantiated {wordObj.GetType().FullName} from CLSID {WORD_CLSID}" 
    
    // Close Word.
    word.InvokeMember("Quit", BindingFlags.InvokeMethod, null, wordObj, [| box 0; 0; false |] ) |> ignore
// The method can throw any of a variety of exceptions.
with e ->
    printfn $"{e.GetType().Name}: Unable to instantiate an object for {WORD_CLSID}" 
// The example displays the following output:
//    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
//    Instantiated Microsoft.Office.Interop.Word.ApplicationClass
Imports System.Reflection
Imports System.Runtime.InteropServices

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

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

注釈

メソッドはGetTypeFromCLSID、COM オブジェクトのクラス識別子 (CLSID) がわかっている場合に、.NET Framework アプリからアンマネージ COM オブジェクトへの遅延バインディング アクセスをサポートします。 COM クラスのクラス識別子は、レジストリのHKEY_CLASSES_ROOT\CLSID キーで定義されます。 プロパティの値を取得して、 IsCOMObject このメソッドによって返される型が COM オブジェクトであるかどうかを判断できます。

ヒント

プログラム識別子 (ProgID) がわかっている COM オブジェクトへの遅延バインディング アクセスのメソッドを呼び出 GetTypeFromProgID すことができます。

CLSID からアンマネージ COM オブジェクトをインスタンス化することは、次の 2 段階のプロセスです。

  1. メソッドを Type 呼び出して、CLSID に対応する を表す __ComObject オブジェクトを GetTypeFromCLSID 取得します。

  2. メソッドを Activator.CreateInstance(Type) 呼び出して COM オブジェクトをインスタンス化します。

などのOutOfMemoryException例外は、 にをtruethrowOnError指定するときにスローされますが、登録されていない CLSID では失敗しません。

注意 (呼び出し元)

このメソッドは、.NET Framework オブジェクトではなく COM オブジェクトを操作する場合に使用することを目的としています。 COM に表示されるオブジェクト (つまり、属性 ComVisibleAttribute が ) を含むすべてのマネージド オブジェクトには true、 プロパティによって GUID 返される GUID があります。 メソッドは GetTypeFromCLSID(Guid, String, Boolean) 、特定のマネージド オブジェクトの GUID に対応するオブジェクトを返 Type しますが、次の例に示すように、 メソッドを呼び出して、その Type オブジェクトを CreateInstance(Type) 使用して型インスタンスを作成することはできません。

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)).
open System
open System.Runtime.InteropServices

[<assembly: ComVisible true>]
do ()

// Define two classes, and assign one an explicit GUID.
[<Guid "d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4">]
type ExplicitGuid() = class end

type NoExplicitGuid() = class end

let explicitType = typeof<ExplicitGuid>
let explicitGuid = explicitType.GUID

// Get type of ExplicitGuid from its GUID.
let explicitCOM = Type.GetTypeFromCLSID explicitGuid
printfn $"Created {explicitCOM.Name} type from CLSID {explicitGuid}"
                
// Compare the two type objects.
printfn $"{explicitType.Name} and {explicitCOM.Name} equal: {explicitType.Equals explicitCOM}"       

// Instantiate an ExplicitGuid object.
try
    let obj = Activator.CreateInstance explicitCOM
    printfn $"Instantiated a {obj.GetType().Name} object"
with :? COMException as e ->
    printfn $"COM Exception:\n{e.Message}\n"

let notExplicit = typeof<NoExplicitGuid>
let notExplicitGuid = notExplicit.GUID

// Get type of ExplicitGuid from its GUID.
let notExplicitCOM = Type.GetTypeFromCLSID(notExplicitGuid)
printfn $"Created {notExplicitCOM.Name} type from CLSID {notExplicitGuid}"
                
// Compare the two type objects.
printfn $"{notExplicit.Name} and {notExplicitCOM.Name} equal: {notExplicit.Equals notExplicitCOM}"       

// Instantiate an ExplicitGuid object.
try
    let obj = Activator.CreateInstance notExplicitCOM
    printfn $"Instantiated a {obj.GetType().Name} object"
with :? COMException as e ->
    printfn $"COM Exception:\n{e.Message}\n"   
// 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)).
Imports System.Runtime.InteropServices

<Assembly:ComVisible(True)>

' Define two classes, and assign one an explicit GUID.
<GuidAttribute("d055cba3-1f83-4bd7-ba19-e22b1b8ec3c4")>
Public Class ExplicitGuid
End Class

Public Class NoExplicitGuid
End Class

Module Example
   Public Sub Main()
      Dim explicitType As Type = GetType(ExplicitGuid)
      Dim explicitGuid As Guid = explicitType.GUID
      
      ' Get type of ExplicitGuid from its GUID.
      Dim explicitCOM As Type = 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 
         Dim obj As Object = Activator.CreateInstance(explicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try 
        
      Dim notExplicit As Type = GetType(NoExplicitGuid)
      Dim notExplicitGuid As Guid = notExplicit.GUID
      
      ' Get type of ExplicitGuid from its GUID.
      Dim notExplicitCOM As Type = 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 
         Dim obj As Object = Activator.CreateInstance(notExplicitCOM)
         Console.WriteLine("Instantiated a {0} object", obj.GetType().Name)
      Catch e As COMException
         Console.WriteLine("COM Exception:{1}{0}{1}", e.Message, vbCrLf)   
      End Try 
   End Sub
End Module
' 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)使用してアンマネージド COM オブジェクトの GUID を取得する必要があります。メソッドにCreateInstance(Type)渡される結果Typeのオブジェクトは、アンマネージ COM オブジェクトを表す必要があります。

適用対象