是否有可能从测试.NET中的COM暴露的组件? [英] Is it possible to test a COM-exposed assembly from .NET?
问题描述
我有我已经通过一个TLB文件暴露给COM .NET程序集,并注册了TLB的安装程序。我已经手动检查安装程序工作正常,并且COM客户端可以访问库。到目前为止,一切都很好...
I have a .NET assembly which I have exposed to COM via a tlb file, and an installer which registers the tlb. I have manually checked that the installer works correctly and that COM clients can access the library. So far, so good...
不过,我试图把一些自动化系统测试,其中检查安装是否正常工作。由于这部分我有自动在虚拟机上安装了,我现在要对已安装的COM库的一些调用,以验证它是否正常工作。我本来以为关于VB6编写一些测试,但我已经有一个大套房的C#编写的测试,引用.NET程序集。我希望我可以改变这些引用.TLB,但我得到一个错误,当我尝试这个VS2008中:
However, I am trying to put together some automated system tests which check that the installer is working correctly. As part of that I have automated the installation on a VM, and I now want to make some calls to the installed COM library to verify that it is working correctly. I originally thought about writing some tests in VB6, but I already have a large suite of tests written in C#, which reference the .NET assembly. I was hoping that I could change these to reference the .tlb, but I get an error when I try this within VS2008:
该ActiveX类型库blah.tlb从.NET程序集导出,并且不能被添加作为一个参考。
The ActiveX type library 'blah.tlb' was exported from a .NET assembly and cannot be added as a reference.
有没有什么办法可以愚弄VS2008到让我添加此引用,也许通过编辑TLB文件?
Is there any way I can fool VS2008 into allowing me to add this reference, perhaps by editing the tlb file?
谷歌搜索一直没有拿出任何解决方案。所有我发现是微软的连接文章称,这是通过设计: HTTP:/ /connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=120882
Googling hasn't come up with any solutions. All I've found is a Microsoft Connect article stating that this is "By Design": http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=120882
推荐答案
在最近的我'已经得到了一个解决方案是类似以下内容:
The Closest I've gotten to a solution is something like the following:
using System;
class ComClass
{
public bool CallFunction(arg1, arg2)
{
Type ComType;
object ComObject;
ComType = Type.GetTypeFromProgID("Registered.ComClass");
// Create an instance of your COM Registered Object.
ComObject = Activator.CreateInstance(ComType);
object[] args = new object[2];
args[0] = arg1;
args[1] = arg2;
// Call the Method and cast return to whatever it should be.
return (bool)ComType.InvokeMember("MethodToCall", BindingFlags.InvokeMethod, null, ComObject, args))
}
}
这是不是很漂亮,但我觉得得到跨越点。当然,你可以把ComObject实例成一个构造函数和包裹调用该对象的其余部分,但可能没有必要的测试代码。
It's not very pretty, but I think gets the point across. You could of course put the ComObject instantiation into a constructor and wrap the rest of the calls to the object, but probably not necessary for test code.
这篇关于是否有可能从测试.NET中的COM暴露的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!