是否可以替换早期绑定的模块
with newer code written in another language if the interface/GUIDs/etc are the same?
I am trying to do this as is detailed in the cousin post:
But I thought I would cut to the chase.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,动态将无济于事,因为您正在谈论的 VB6 应用程序引用了 VB6(或其他内容)中的 COM 组件,但您现在希望成为 VB.net(或 C#)。
尽管如此,你应该能够做到这一点。
您需要使用 OLEView(或类似的东西)为您要替换的 COM DLL 生成 MIDL。这将为您提供该 DLL 中定义的所有类和接口的特定 GUIDS。
然后,您需要编写 .net 版本的 DLL,为每个类和接口指定每个 GUID。
您需要查看 .net 属性的文档:
ComVisible
指导
InterfaceType
以及可能的更多类型。这些属性允许您专门指出 dll 中的哪些对象和接口使用哪些 GUID。
基本上,您想要的是当您为两个 DLL(旧的和新的)生成 Typelib 时,您最终应该得到相同的 tlb。如果不这样做,新的引用将不会与旧的引用兼容。
Dynamic won't help in this case, since you're talking about a VB6 app referencing a COM component that WAS in VB6 (or something else) but that you NOW want to be VB.net (or C#).
That said though, you should be able to do this.
You'll need to use OLEView (or something similiar) to generate the MIDL for the COM DLL you're replacing. That will give you the specific GUIDS for all the classes and interfaces defined in that DLL.
Then you'll need to code up you .net version of the DLL, specifing EVERY SINGLE GUID for EVERY class and interface.
You'll want to look at the docs for the .net attributes:
ComVisible
Guid
InterfaceType
and possibly a few more. These attrs allow you to specifically notate what GUIDS to use for what objects and interfaces in your dll.
Basically, what you're gunning for is when you generate the Typelibs for the two DLLs (the old and the new), you should end up with identical tlbs. If you don't, the new one won't be early bound reference compatible with the old one.
您可以使用 C# 4.0 的
dynamic
来解决许多此类与 COM 通信的问题。引入动态对象和可选参数来缓解其中的一些麻烦。
You can use C# 4.0's
dynamic
to solve many of such problems with the problems communicating with COM.dynamic
objects and optional parameters were introduced to ease some of these pains.