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

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

问题描述

我有一个.NET程序集,我通过一个tlb文件暴露给COM和一个安装程序注册tlb。我已手动检查安装程序是否正常工作,并且COM客户端可以访问库。到目前为止,这么好...



然而,我试图把一些自动化系统测试,检查安装程序是否正常工作。作为其中的一部分,我已经在VM上自动化安装,现在我想对安装的COM库进行一些调用,以验证它是否正常工作。我最初想到在VB6中编写一些测试,但我已经有一个大套的测试用C#编写,它引用.NET程序集。我希望我可以改变这些参考.tlb,但我得到一个错误,当我尝试这VS2008:



ActiveX类型库'blah.tlb'是从.NET程序集导出的,无法作为参考添加。



是否有任何方法可以愚弄VS2008允许我添加这个引用,也许通过编辑tlb文件?



Google搜索没有找到任何解决方案。我找到的是一个Microsoft Connect文章,说明这是按设计: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=120882

解决方案

我得到一个解决方案的最接近的是如下:

  using System; 
class ComClass
{
public bool CallFunction(arg1,arg2)
{
类型ComType;
object ComObject;

ComType = Type.GetTypeFromProgID(Registered.ComClass);
//创建一个COM注册对象的实例。
ComObject = Activator.CreateInstance(ComType);

object [] args = new object [2];
args [0] = arg1;
args [1] = arg2;

//调用方法并将返回值转换为任何值。
return(bool)ComType.InvokeMember(MethodToCall,BindingFlags.InvokeMethod,null,ComObject,args))
}
}
pre>

这不是很漂亮,但我认为。你当然可以把ComObject实例化到一个构造函数中,并将其余的调用包装到对象中,但是测试代码可能不需要。


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...

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:

The ActiveX type library 'blah.tlb' was exported from a .NET assembly and cannot be added as a reference.

Is there any way I can fool VS2008 into allowing me to add this reference, perhaps by editing the tlb file?

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))
    }
}

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天全站免登陆