Gettypefromprogid() Method In C#

Within this guide, you will gain insights into the GetTypeFromProgID Method in C#, including its syntax, parameters, and a demonstration.

What is the GetTypeFromProgID method?

The GetTypeFromProgID function is employed to retrieve the type details for a Component Object Model (COM) instance in C#. This is based on the ProgID (programmatic identifier) of the object. ProgID serves as a user-friendly identifier linked to a COM object that's registered in the Windows Registry. By utilizing this function, programmers can interact with COM objects even if they are unaware of the object types during compilation, thanks to dynamic type resolution during program execution.

Programmers can employ a COM object's ProgID to dynamically retrieve the correct Type object rather than directly specifying the type of a COM object in the 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 signifies the type details for the designated COM object. If the ProgID cannot be retrieved or located, this function will return null.

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's consider a scenario to demonstrate the implementation of the GetTypeFromProgID function 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:

This code showcases the process of leveraging a COM object's ProgID dynamically in C#. It involves retrieving type details, instantiating the object, calling a method, and retrieving a property value. The implementation includes robust error handling to manage any potential exceptions effectively.

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:

The type information of a COM (Component Object Model) object can be obtained by utilizing the "Type.GetTypeFromProgID(String progID, String server)" function in C#. This function specifically pinpoints the ProgID (Programmatic Identifier) of the COM object and the server's designation where it is located.

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 summary, the provided code excerpt illustrates the process of dynamically retrieving the type information of a COM object using its ProgID. It also offers the choice to specify the server hosting the COM object. By implementing thorough error-handling mechanisms, it allows for versatile access to COM objects from both local and remote systems.

Input Required

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