Gettypefromprogid() Method In C#

In this article, you will learn about the GetTypeFromProgID Method in C# with its syntax, parameters, and example.

What is the GetTypeFromProgID method?

The GetTypeFromProgID method is used to get the type information for a COM (Component Object Model) object in C#, which is based on the programmatic identifier (ProgID) of the object. ProgID is a human-readable identifier associated with a COM object registered in the Windows Registry. Using this method, developers may deal with COM objects without knowing the types of those objects at compile time because of dynamic type resolution at runtime.

Developers may utilize a COM object's ProgID to dynamically acquire the appropriate Type object instead of explicitly referencing a COM object's type in code.

Syntax:

It has the following syntax:

Example

public static Type GetTypeFromProgID(string program_ID)
  • public: This method can be accessed outside of the class.
  • static: This method is part of the Type class rather than being unique to one class instance.
  • Type: It is the method's return type. It displays the type information for the COM object that the Program_ID has recognized.
  • GetTypeFromProgID: The name of the method is GetTypeFromProgID .
  • (string programID): The parameter list is included in string programID. It specifies the ProgramID of the COM object whose type information you want to acquire. programID is the COM object's ProgID.
  • Return Value:

The Type object represents the type information for the specified COM object. This method returns null if the ProgID cannot be obtained or found.

Exceptions

  • A null ArgumentException is raised if the ProgID argument is null.
  • Null is returned, and no exception is raised if the specified ProgID cannot be found or accessed.
  • GetTypeFromProgID uses reflection, an effective runtime tool for examining and modifying types and objects.
  • Developers can search for type metadata, dynamically create instances, call methods, access properties, and perform other operations via reflection.
  • Example-1:

Let us take an example to illustrate the GetTypeFromProgID method in C#.

Example

using System;
class Demo
{
    static void Main()
    {
        try
        {
            // Specify the COM object you want to work with its Program_ID.
            string program_ID = "Shell.Application";
            // Using the COM object's ProgID, retrieve its type information.
            Type t = Type.GetTypeFromProgID(program_ID);
            if (t != null)
            {
                // Create an instance of the COM object
                object com_Object = Activator.CreateInstance(t);
                // Invoke a method to demonstrate its usage.
                var windows = t.InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, com_Object, null);
                Console.WriteLine("Number of open windows: " + windows.GetType().InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null));
            }
            else
            {
                Console.WriteLine("The type information for the supplied ProgID could not be retrieved.");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("An error has occurred: " + e.Message);
        }
    }
}

Output:

Output

An error has occurred: Unmanaged activation is not supported

Explanation:

Overall, this code demonstrates how to use a COM object's ProgID to dynamically interact with it in C#, including collecting type information, creating an instance, invoking a method, and accessing a property. By easily handling any exceptions that may occur during execution, error handling ensures robustness.

Usage:

  • When interacting with COM objects dynamically, developers usually choose this approach.
  • Common cases include system service access, legacy COM component integration, and automation of external programs (like Microsoft Office).
  • Creating instances of the COM object, invoking its methods, and dynamically accessing its attributes are all possible using the acquired Type object.
  • Example-2:

A COM (Component Object Model) object's type information may be retrieved using the "Type.GetTypeFromProgID(String progID, String server)" method in C#. This method identifies the Programmatic Identifier (ProgID) of the COM object and the server's name on which it resides.

Example

using System;

class Demo
{
    static void Main()
    {
        try
        {
            // Specify the ProgID of the COM object and the name of the server
            string program_ID = "Shell.Application";
            string server = null; 
            // In this case, you can provide the remote server's name where the COM object resides.
           // Using the COM object's ProgID and server name, retrieve its type information.
            Type t = Type.GetTypeFromProgID(program_ID, server);
            if (t != null)
            {
                // Type information obtained, and proceed with further actions...
                Console.WriteLine($"Type information for ProgID '{program_ID}' retrieved successfully.");
            }
            else
            {
                Console.WriteLine("The type information for the supplied ProgID could not be retrieved.");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("An error has occurred: " + e.Message);
        }
    }
}

Output:

Output

An error has occurred: Unmanaged activation is not supported

Conclusion:

In conclusion, the code snippet demonstrates how to dynamically obtain a COM object's type information based on its ProgID , with the option to include the server where the COM object resides. While ensuring robust error handling, it provides flexibility in accessing COM objects from both local and remote machines.

Input Required

This code uses input(). Please provide values below: