C# 实现本机代码的后期绑定
我们正在使用现有的本机应用程序(很可能是用 VB 编写的),该应用程序加载程序集并使用“后期绑定”调用方法。我们无法访问其源代码。
我们希望在 C# 中实现此接口,并让本机应用程序调用我们的 C# 程序集。
这有可能吗?
除了匹配方法名称和方法签名才能使其正常工作之外,我们还需要做些什么吗?
We are working with an existing native application (most likely written in VB) that loads assemblies and calls methods with "Late Binding." We do NOT have access to its source code.
We want to implement this interface in C#, and have the native application call our C# assembly.
Is this something that's possible?
Is this anything we have to do beyond matching the method names and method signatures to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试从 VB6 调用 .NET 代码,最简洁的方法是使 C# 代码显示为 VB6 代码的 COM 对象 - 所以,是的,您需要将 C# 代码标记为
ComVisible 并确保它看起来像现有的 COM 接口。
更新: 这里有一篇文章可以帮助您入门。
If you're trying to call .NET code from VB6 the cleanest way would be to make the C# code appear as a COM object to the VB6 code - so yes, you would need to mark your C# code as
ComVisible
and ensure that it looks like the existing COM interface.Update: Here's an article to get you started.
以下是使其正常工作所需的步骤:
[ComVisible(true)]
,并确保为其提供唯一的[Guid]
属性ProgId
到您的类,通常是MyNamespace.MyClass
,但您也可以在类上添加一个属性来覆盖它[DispId]
将调用的每个方法的属性regasm.exe
瞧!本机代码可以通过“后期绑定”调用您的 C#。当然,我必须设置几个注册表项才能使我的本机应用程序知道如何加载我的程序集,但一切正常。
These are the steps required to make it work:
[ComVisible(true)]
, and make sure to give it a unique[Guid]
attributeProgId
to your class which is normallyMyNamespace.MyClass
, but you can add an attribute on your class to override this as well[DispId]
attributes for each method that will be calledregasm.exe
on your assemblyAnd voila! Native code can call your C# via "late binding". I, of course, had to setup several registry keys to make my native application know how to load my assembly, but all is working.