System.Type.GetTypeFromProgID 方法 (String, String, Boolean)

方法描述

从指定服务器获取与指定程序标识符 (progID) 关联的类型,指定如果在加载该类型时发生错误是否引发异常。

语法定义(C# System.Type.GetTypeFromProgID 方法 (String, String, Boolean) 的用法)

public static Type GetTypeFromProgID(
	string progID,
	string server,
	bool throwOnError
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
progID System-String 要获取的 Type 的 progID。
server System-String 用于从中加载该类型的服务器。如果服务器名称为 null,则此方法会自动恢复到本地计算机上。
throwOnError System-Boolean true 将引发所发生的任何异常。- 或 -false 将忽略所发生的任何异常。
返回值 System.Type 如果 progID 是注册表中的有效项且有与之关联的类型,则为与指定程序标识符 (ProgID) 关联的类型;否则为 null。

提示和注释

此方法是为 COM 支持提供的。 Microsoft .NET Framework 不使用程序 ID,因为程序 ID 已被命名空间的概念所取代。

System.Type.GetTypeFromProgID 方法 (String, String, Boolean)例子

然后,该示例显示与 ProgID 相关的 ClassID,指定 ProgID 或服务器名称无效时是否引发异常。

using System;
class MainApp 
{
    public static void Main()
    {
        try
        {
            // Use server localhost.
            string theServer="localhost";
            // Use  ProgID HKEY_CLASSES_ROOT\DirControl.DirList.1.
            string myString1 ="DirControl.DirList.1"; 
            // Use a wrong ProgID WrongProgID.
            string myString2 ="WrongProgID"; 
            // Make a call to the method to get the type information for the given ProgID.
            Type myType1 =Type.GetTypeFromProgID(myString1,theServer,true);
            Console.WriteLine("GUID for ProgID DirControl.DirList.1 is {0}.", myType1.GUID);
            // Throw an exception because the ProgID is invalid and the throwOnError 
            // parameter is set to True.
            Type myType2 =Type.GetTypeFromProgID(myString2, theServer, true);
        }
        catch(Exception e)
        {
            Console.WriteLine("An exception occurred. The ProgID is wrong.");
            Console.WriteLine("Source: {0}" , e.Source);
            Console.WriteLine("Message: {0}" , e.Message);
        }
    }
}

异常

异常 异常描述
ArgumentException progID 为 null。
COMException 指定的 ProgID 没有注册。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。