在网上查了一下,应该是不能直接在WinForm中使用C#写的COM控件。
但可以用变通的方法测试COM控件中的方法,比如,下面的例子中,就可以测试addTest方法:
Type comType = null;
object comObject = null;
//这个无效,不知道为什么
//comType = Type.GetTypeFromProgID("XX.CSControl.TestControl");
if (comType == null)
{
Guid guid = new Guid("4B4D1D4C-16CA-48E0-87A4-AFF3C6CB6E26");
comType = Type.GetTypeFromCLSID(guid);
}
try
{
comObject = Activator.CreateInstance(comType);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
return;
}
object[] args = new object[2];
args[0] = 1;
args[1] = 2;
int ret = (int)comType.InvokeMember("addTest", BindingFlags.InvokeMethod, null, comObject, args);
if(ret==3)
{
MessageBox.Show("TestOK");
}