调用方式
例1:
using System;
//引用命名空间
using System.Reflection;
class program
{
static void Main(string[] args)
{
System.Guid guid = new Guid("3ba6d31e-c449-4e14-a5c1-f9218ec1338c");
//Type com1 = Type.GetTypeFromCLSID(guid, true);
Type com = Type.GetTypeFromProgID("MyLib.MyClass");
//创建其对象
if (com!=null)
{
object myobject = Activator.CreateInstance(com);
//设置调用参数
object[] parameter = new object[] { "1", "2" };
com.InvokeMember("ShowText", BindingFlags.Default|BindingFlags.InvokeMethod, null,
myobject , parameter);
}
Console.ReadLine();
}
}
例2:手动添加(使用加引用选项卡来添加COM组件)
//在程序代码编写前先引用微软的 Excel COM 组件
using System;
using System.Reflection;
using System.Threading;
using Excel;
class program
{
static void Main(string[] args)
{
TestComLibClass tc = new TestComLibClass();
tc.ShowText("3", "4");
Console.ReadLine();
}
}