Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Equivalent of JScript's new ActiveXObject

I am trying to use the functionality of new ActiveXObject() found in JScript.NET in C#. How can I do this?

And don't say that anything you can do with COM objects can be done in C#. If I wanted to do it that way, I already would have.

like image 228
Bob Avatar asked Feb 16 '23 20:02

Bob


1 Answers

You can create instances of COM objects using

Activator.CreateInstance(Type.GetTypeFromProgID(ProgID))

and then work with them using late binding. For example:

using System.Reflection;
...

Type wshType = Type.GetTypeFromProgID("WScript.Shell");
object wshShell = Activator.CreateInstance(wshType);
wshType.InvokeMember("Popup", BindingFlags.InvokeMethod, null, wshShell, new object[] {"Hello, world!"});

or, using C# 4's dynamic keyword:

// NB: Add reference to Microsoft.CSharp.dll
dynamic wshShell = Activator.CreateInstance(Type.GetTypeFromProgID("WScript.Shell"));
wshShell.Popup("Hello, world!");
like image 180
Helen Avatar answered Feb 19 '23 10:02

Helen