다음을 통해 공유


Type.GetTypeFromCLSID 메서드

정의

지정된 CLSID(클래스 식별자)와 연관된 형식을 가져옵니다.

오버로드

GetTypeFromCLSID(Guid)

지정된 CLSID(클래스 식별자)와 연관된 형식을 가져옵니다.

GetTypeFromCLSID(Guid, Boolean)

지정된 CLSID(클래스 식별자)와 연관된 형식을 가져오고 형식을 로드하는 동안 오류가 발생하면 예외를 throw할지를 지정합니다.

GetTypeFromCLSID(Guid, String)

지정된 CLSID(클래스 식별자)와 연관된 형식을 지정된 서버에서 가져옵니다.

GetTypeFromCLSID(Guid, String, Boolean)

지정된 CLSID(클래스 식별자)와 연관된 형식을 지정된 서버에서 가져오고 형식을 로드하는 동안 오류가 발생하면 예외를 throw할지를 지정합니다.

GetTypeFromCLSID(Guid)

Source:
Type.cs
Source:
Type.cs
Source:
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입니다.

반환

System.__ComObject CLSID가 유효한지 여부에 관계 없습니다.

특성

예제

다음 예제에서는 Microsoft Word의 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) 오버로드는 인수에 따라 개체를 인스턴스화할 때 발생할 수 있는 예외를 Typeclsid 무시합니다. 레지스트리에서 를 찾을 수 없는 경우 clsid 예외가 throw되지 않습니다.

호출자 참고

이 메서드는 .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를 검색하는 Type 데만 사용해야 하며, 메서드에 CreateInstance(Type) 전달되는 결과 개체는 관리되지 않는 COM 개체를 나타내야 합니다.

적용 대상

GetTypeFromCLSID(Guid, Boolean)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

지정된 CLSID(클래스 식별자)와 연관된 형식을 가져오고 형식을 로드하는 동안 오류가 발생하면 예외를 throw할지를 지정합니다.

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

발생하는 예외를 모두 throw하려면 true입니다.

또는

false는 발생하는 예외를 모두 무시합니다.

반환

System.__ComObject CLSID가 유효한지 여부에 관계 없습니다.

특성

예제

다음 예제에서는 Microsoft Word의 CLSID 애플리케이션 개체 Microsoft Word 애플리케이션을 나타내는 COM 종류를 검색 합니다. 그런 다음 메서드를 호출하여 형식을 Activator.CreateInstance 인스턴스화하고 Application.Quit 메서드를 호출하여 닫습니다. 형식을 로드하는 동안 오류가 발생하면 예외가 throw됩니다.

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 개체를 인스턴스화합니다.

그림의 예제를 참조하세요.

에 를 지정할 truethrowOnError때 와 같은 OutOfMemoryException 예외가 throw되지만 등록되지 않은 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를 검색하는 Type 데만 사용해야 하며, 메서드에 CreateInstance(Type) 전달되는 결과 개체는 관리되지 않는 COM 개체를 나타내야 합니다.

적용 대상

GetTypeFromCLSID(Guid, String)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

지정된 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이면 이 메서드는 자동으로 로컬 컴퓨터로 전환됩니다.

반환

System.__ComObject CLSID가 유효한지 여부에 관계 없습니다.

특성

예제

다음 예제에서는 Microsoft Word의 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를 검색하는 Type 데만 사용해야 하며, 메서드에 CreateInstance(Type) 전달되는 결과 개체는 관리되지 않는 COM 개체를 나타내야 합니다.

적용 대상

GetTypeFromCLSID(Guid, String, Boolean)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

지정된 CLSID(클래스 식별자)와 연관된 형식을 지정된 서버에서 가져오고 형식을 로드하는 동안 오류가 발생하면 예외를 throw할지를 지정합니다.

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

발생하는 예외를 모두 throw하려면 true입니다.

또는

false는 발생하는 예외를 모두 무시합니다.

반환

System.__ComObject CLSID가 유효한지 여부에 관계 없습니다.

특성

예제

다음 예제에서는 Microsoft Word의 CLSID 애플리케이션 개체 computer17.central.contoso.com 라는 서버에서 Microsoft Word 애플리케이션을 나타내는 COM 종류를 검색 합니다. 그런 다음 메서드를 호출하여 형식을 Activator.CreateInstance 인스턴스화하고 Application.Quit 메서드를 호출하여 닫습니다. 형식을 로드하는 동안 오류가 발생하면 예외가 throw됩니다.

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 개체를 인스턴스화합니다.

에 를 지정할 truethrowOnError때 와 같은 OutOfMemoryException 예외가 throw되지만 등록되지 않은 CLSID에는 실패하지 않습니다.

호출자 참고

이 메서드는 .NET Framework 개체가 아닌 COM 개체로 작업할 때 사용하기 위한 것입니다. COM에 표시되는 개체(즉, ComVisibleAttribute 특성 true은 )를 포함한 모든 관리되는 개체에는 속성에서 반환되는 GUID가 GUID 있습니다. 메서드는 GetTypeFromCLSID(Guid, String, Boolean) 특정 관리되는 개체의 GUID에 해당하는 개체를 반환 Type 하지만 다음 예제와 같이 메서드를 호출 CreateInstance(Type) 하여 형식 인스턴스를 만드는 데는 해당 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를 검색하는 Type 데만 사용해야 하며, 메서드에 CreateInstance(Type) 전달되는 결과 개체는 관리되지 않는 COM 개체를 나타내야 합니다.

적용 대상