GetTypeFromCLSID Methode (Guid)
Inhaltsverzeichnis reduzieren
Inhaltsverzeichnis erweitern
Dieser Artikel wurde maschinell übersetzt. Wenn Sie die englische Version des Artikels anzeigen möchten, aktivieren Sie das Kontrollkästchen Englisch. Sie können den englischen Text auch in einem Popupfenster anzeigen, indem Sie den Mauszeiger über den Text bewegen.
Übersetzung
Englisch

Type.GetTypeFromCLSID-Methode (Guid)

 

Ruft den Typ ab, dem die angegebene Klassen-ID (CLSID) zugeordnet ist.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static Type GetTypeFromCLSID(
	Guid clsid
)

Parameter

clsid
Type: System.Guid

Die CLSID des abzurufenden Typs.

Rückgabewert

Type: System.Type

System.__ComObject unabhängig von der Gültigkeit der CLSID.

The M:System.Type.GetTypeFromCLSID(System.Guid) method supports late-bound access to unmanaged COM objects from .NET Framework apps when you know the COM object's class identifier (CLSID). The class identifier for COM classes is defined in the HKEY_CLASSES_ROOT\CLSID key of the registry. You can retrieve the value of the P:System.Type.IsCOMObject property to determine whether the type returned by this method is a COM object.

System_CAPS_tipTipp

You can call the M:System.Type.GetTypeFromProgID(System.String) method for late-bound access to COM objects whose programmatic identifier (ProgID) you know.

Instantiating an unmanaged COM object from its CLSID is a two-step process:

  1. Get a T:System.Type object that represents the__ComObject that corresponds to the CLSID by calling the M:System.Type.GetTypeFromCLSID(System.Guid) method.

  2. Call the M:System.Activator.CreateInstance(System.Type) method to instantiate the COM object.

See the example for an illustration.

The M:System.Type.GetTypeFromCLSID(System.Guid) overload ignores any exception that may occur when instantiating a T:System.Type object based on the clsid argument. Note that no exception is thrown if clsid is not found in the registry.

Hinweise zu Aufrufern:

This method is intended for use when working with COM objects, not with .NET Framework objects. All managed objects, including those that are visible to COM (that is, their T:System.Runtime.InteropServices.ComVisibleAttribute attribute is true) have a GUID that is returned by the P:System.Type.GUID property. Although the method returns a T:System.Type object that corresponds to the GUID for .NET Framework objects, you can't use that T:System.Type object to create a type instance by calling the M:System.Activator.CreateInstance(System.Type) method, as the following example shows.

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)).

Instead, the M:System.Type.GetTypeFromCLSID(System.Guid,System.String,System.Boolean) should only be used to retrieve the GUID of an unmanaged COM object, and the resultingT:System.Type object that is passed to the M:System.Activator.CreateInstance(System.Type)method must represent an unmanaged COM object.

The following example uses the CLSID of the Microsoft Word Application objecthttp://msdn.microsoft.com/library/office/ff838565.aspx to retrieve a COM type that represents the Microsoft Word application. It then instantiates the type by calling the M:System.Activator.CreateInstance(System.Type) method, and closes it by calling the Application.Quithttp://msdn.microsoft.com/library/office/ff844895.aspx method.

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

public class Example
{
   private const string WORD_CLSID = "{000209FF-0000-0000-C000-000000000046}";

   public static void Main()
   {
      // Start an instance of the Word application.
      var word = Type.GetTypeFromCLSID(Guid.Parse(WORD_CLSID));
      Console.WriteLine("Instantiated Type object from CLSID {0}",
                        WORD_CLSID);
      Object wordObj = Activator.CreateInstance(word);
      Console.WriteLine("Instantiated {0}", 
                        wordObj.GetType().FullName);

      // Close Word.
      word.InvokeMember("Quit", BindingFlags.InvokeMethod, null, 
                        wordObj, new object[] { 0, 0, false } );
   }
}
// The example displays the following output:
//    Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
//    Instantiated Microsoft.Office.Interop.Word.ApplicationClass

.NET Framework
Verfügbar seit 1.1
Portable Class Library
Unterstützt in: portable .NET platforms
Zurück zum Anfang
Anzeigen:
© 2016 Microsoft