是否可以从 .NET 测试 COM 公开的程序集? [英] Is it possible to test a COM-exposed assembly from .NET?

查看:16
本文介绍了是否可以从 .NET 测试 COM 公开的程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过 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...

但是,我正在尝试组合一些自动化系统测试来检查安装程序是否正常工作.作为其中的一部分,我已经在 VM 上自动安装,现在我想对已安装的 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?

谷歌搜索没有提出任何解决方案.我发现的只是一篇 Microsoft Connect 文章,说明这是按设计":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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆