当前位置: 首页>>代码示例>>C#>>正文


C# Type.GetTypeFromCLSID方法代码示例

本文整理汇总了C#中System.Type.GetTypeFromCLSID方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetTypeFromCLSID方法的具体用法?C# Type.GetTypeFromCLSID怎么用?C# Type.GetTypeFromCLSID使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Type的用法示例。


在下文中一共展示了Type.GetTypeFromCLSID方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
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);
      }      
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:33,代码来源:Type.GetTypeFromCLSID

输出:

Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
Instantiated Microsoft.Office.Interop.Word.ApplicationClass

示例2: Main

//引入命名空间
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);   
      }   
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:63,代码来源:Type.GetTypeFromCLSID

输出:

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

示例3: Main

//引入命名空间
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.");   
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:29,代码来源:Type.GetTypeFromCLSID

输出:

Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
Instantiated Microsoft.Office.Interop.Word.ApplicationClass

示例4: Main

//引入命名空间
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);
      }
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:29,代码来源:Type.GetTypeFromCLSID

输出:

Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
Instantiated Microsoft.Office.Interop.Word.ApplicationClass

示例5: Main

//引入命名空间
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 } );
   }
}
开发者ID:.NET开发者,项目名称:System,代码行数:24,代码来源:Type.GetTypeFromCLSID

输出:

Instantiated Type object from CLSID {000209FF-0000-0000-C000-000000000046}
Instantiated Microsoft.Office.Interop.Word.ApplicationClass


注:本文中的System.Type.GetTypeFromCLSID方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。