Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manual way to call COM object through .NET

Is there a manual way to call a COM object in the GAC in .NET, without adding it as a reference?

The reason I ask is I only know how to code in C# and want to call a .NET COM object and tests that its CMO calls are visible, but obviously you can't add a .NET COM object to a .NET assembly! As you have to reference it, so I was wondering can you call it if its registered in the GAC manually through c# code?

like image 459
David Avatar asked Oct 28 '09 12:10

David


1 Answers

Type myType = Type.GetTypeFromProgID("IMyLib.MyClass");
object obj = Activator.CreateInstance(myType);
object[] args = new object[2];
args[0] = "Hello";
args[1] = 3;
myType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, obj, args);

In .Net 4 something like this

Type myType = Type.GetTypeFromProgID("IMyLib.MyClass");
dynamic obj = Activator.CreateInstance(myType);
obj.MyMethod("Hello", 3);
like image 161
Arsen Mkrtchyan Avatar answered Oct 07 '22 11:10

Arsen Mkrtchyan