实现本机代码的后期绑定

实现本机代码的后期绑定

本文介绍了C#实现本机代码的后期绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用现有的本机应用程序(最有可能用VB编写),该应用程序加载程序集并使用后期绑定"调用方法.我们无权访问其源代码.

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.

我们想用C#实现此接口,并让本地应用程序调用我们的C#程序集.

We want to implement this interface in C#, and have the native application call our C# assembly.

这可能吗?

除了匹配方法名称和方法签名以使其起作用之外,我们还需要做其他事情吗?

Is this anything we have to do beyond matching the method names and method signatures to make it work?

推荐答案

以下是使其工作所需的步骤:

These are the steps required to make it work:

  1. 标记您的课程[ComVisible(true)],并确保为其赋予唯一的[Guid]属性
  2. ProgId匹配到通常是MyNamespace.MyClass的类上,但是您可以在类上添加一个属性来覆盖它
  3. 为将要调用的每种方法放置适当的[DispId]属性
  4. 编译程序集并在程序集上运行regasm.exe
  1. Mark your class [ComVisible(true)], and make sure to give it a unique [Guid] attribute
  2. Match your ProgId to your class which is normally MyNamespace.MyClass, but you can add an attribute on your class to override this as well
  3. Put the proper [DispId] attributes for each method that will be called
  4. Compile your assembly and run regasm.exe on your assembly

瞧!本机代码可以通过后期绑定"调用您的C#.我当然必须设置几个注册表项,以使本机应用程序知道如何加载程序集,但是一切正常.

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

这篇关于C#实现本机代码的后期绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:15