How to invoke a method on a COM object in C#
Where I work we often need to invoke methods on a COM object that has been developed in VB6 or in .NET. I wrote this helper class for speed it along.
public static class ActiveX
{
public static object Invoke(string @namespace, string @class, string method, params object[] parameters)
{
var progId = string.Format("{0}.{1}", @namespace, @class);
return Invoke(progId, method, parameters);
}
public static object Invoke(string progId, string method, params object[] parameters)
{
var type = Type.GetTypeFromProgID(progId);
var comObject = Activator.CreateInstance(type);
return type.InvokeMember(method, BindingFlags.InvokeMethod, null, comObject, parameters);
}
}