Javatpoint Logo
Javatpoint Logo

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:

  • 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 program_ID): The parameter list is included in string program_ID. It specifies the Program_ID of the COM object whose type information you want to acquire. program_ID 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#.

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:

  1. When interacting with COM objects dynamically, developers usually choose this approach.
  2. Common cases include system service access, legacy COM component integration, and automation of external programs (like Microsoft Office).
  3. 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.

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.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA