C#

C# Create Instance

Posted by Kerwen Blog on November 30, 2023

For Late binding we have to use System.Reflection namespace, which allows us to programmatically access the types contained in any assembly. We will see how we can do late bindings in four easy steps..

  • We have Get IDispatch Interface using Type.GetTypeFromProgID(“Project1.Class1”)
  • We have to create instance using the type ID Activator.CreateInstance(objAddType)
  • We have to make array of arguments (if required)
  • Invoke the Method using objAddType.InvokeMember function.

image

The method Type.GetTypeFromProgID is used to load the type information of the COM object. The call to Activator.CreateInstance returns an instance of the COM object. And Finally InvokeMember function is used to call the Method of COM object.