Unable to cast COM object of type 'System.__ComObject' to class type 的解决办法

来源:互联网 发布:学长笛的软件 编辑:程序博客网 时间:2024/05/05 21:56

正在开发一个自动提交表单的程序,里面用到了WebBrowser控件。开始用这个控件时真是有点慌也是好奇,因为我对能够实现自动提交表单很感兴趣,开始不知如何下手。查了资料,里面说了一堆东西COM如何如何,让我头疼,毕竟我这种从开始就是用.net的程序员大多数对COM里面的不深刻。还好吧,总算克服很多困难做出来一些了。但是又在发布的时候遇到困难。奇怪的是在开发环境是正常的,这很让人认为是客户机器上少装了什么组件。

我遇到的具体错误是:

Unable to cast COM object of type 'System.__ComObject' to class type

'mshtml.HTMLDocumentClass'. COM components that enter the CLR and do not support

IProvideClassInfo or that do not have any interop assembly registered will be wrapped in the

__ComObject type. Instances of this type cannot be cast to any other class; however they can

be cast to interfaces as long as the underlying COM component supports QueryInterface calls

for the IID of the interface.

 

这段说的太啰嗦了,俺们英文不太好,理解起来还挺费劲的。

其实就是说对于COM对象,不能转化为类类型使用,而是要转化为其所继承的接口使用,就OK了!

果然,我把HTMLDocumentClass换为HTMLDocument(这是一个接口,尽管它的命名没有符合MS的命名规范)一切都正常了。

但是我心中尚有疑问没有解决:为什么我的开发环境上没有这个错误呢!?相信我对这个问题还是没有深入的理解,

如果您有一些见解!非常欢迎留言!谢谢!