【问题标题】:ObjectForScripting with multiple Interfaces not working具有多个接口的 ObjectForScripting 不起作用
【发布时间】:2011-07-02 17:01:15
【问题描述】:

我有一个通过 ObjectForScripting 与 WebBrowserControl 交互的 WinForm。我的 WinForm 的基类不是 ComVisible,我不能或不会更改它。因为有一个 NonComVisibleBaseClass 我创建了一个接口并将其设置为 ComVisible(true) 并设置 FormAttribute [ClassInterface(ClassInterfaceType.None)]。接口中的方法可以被 JavaScript 调用。而且效果很好:

//Make the class visible for COM so we can set the ObjectForScripting
//Specify ClassInterfaceType.None to use the ComVisible Interface
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public partial class GeekBrowser : GeekBasePage, IMapControlInteractable
...
public class GeekBasePage : System.Windows.Forms.Form
...
[ComVisible(true)]
public interface IMapControlInteractable

但现在我的问题。该接口包含多个功能。我想为单独的任务分组分离接口。所以我想要一个包含日志记录功能的接口和一个用于数据访问功能的接口等等。

所以应该是这样的:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public partial class GeekBrowser : GeekBasePage, IDataAccess, ILogging
...
public class GeekBasePage : System.Windows.Forms.Form
...
[ComVisible(true)]
public interface IDataAccess
...
[ComVisible(true)]
public interface ILogging

但是当我这样做时,无法从 Javascript 访问第二个接口 (ILogging) 的功能。如果我切换接口的顺序,则无法访问 IDataAccess 函数。

所以似乎只有第一个接口中的方法可以在 Javascript 中访问。

我可以做些什么来使每个接口的每个功能都可以访问?再一次,使 BaseClass ComVisible 和删除 ClassInterface 属性将起作用,但不是一个选项。

提前致谢!!

【问题讨论】:

    标签: c# javascript winforms interface comvisible


    【解决方案1】:

    在做一个类似的项目时,我们发现 JavaScript 只能访问生成 COM 包装器的默认接口,在您的情况下,它选择它找到的第一个 ComVisible 接口作为默认接口,因为您没有明确设置默认接口属性。问题是 JavaScript 没有 QueryInterface 类似物。

    为了访问其他接口,我们需要为 JavaScript 创建我们自己的 QueryInterface 版本,或者通过在默认接口中提供显式转换类型函数(不是那么优雅)或者拥有一个可以执行转换的单独对象到正确的 ComVisible 接口类型。

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-22
      • 2015-12-14
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      相关资源
      最近更新 更多