C# Type GetTypeFromProgID(String, Boolean)

Description

Type GetTypeFromProgID(String, Boolean) gets the type associated with the specified program identifier (ProgID), specifying whether to throw an exception if an error occurs while loading the type.

Syntax

Type.GetTypeFromProgID(String, Boolean) has the following syntax.


public static Type GetTypeFromProgID(
  string progID,
  bool throwOnError
)

Parameters

Type.GetTypeFromProgID(String, Boolean) has the following parameters.

  • progID - The ProgID of the type to get.
  • throwOnError - true to throw any exception that occurs.
  • throwOnError - -or-
  • throwOnError - false to ignore any exception that occurs.

Returns

Type.GetTypeFromProgID(String, Boolean) method returns The type associated with the specified program identifier (ProgID), if progID is a valid entry in the registry and a type is associated with it; otherwise, null.

Example

The following example retrieves a type by passing a ProgID, specifying whether to throw an exception if the ProgID is invalid.


/*from   w  w w  . j  a va2 s.  co  m*/
using System;
class MainApp 
{
    public static void Main()
    {
        try
        {
            string myString1 ="DIRECT.ddPalette.3"; 
            string myString2 ="MyProgID"; 
            Type myType1 =Type.GetTypeFromProgID(myString1,true);
            Console.WriteLine(myType1.GUID);
            Type myType2 =Type.GetTypeFromProgID(myString2,true);
        }
        catch(Exception e)
        {
            Console.WriteLine("An exception occurred.");
            Console.WriteLine("Source: {0}", e.Source);
            Console.WriteLine("Message: {0}", e.Message);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version