具有多个接口的ObjectForScripting不起作用

时间:2011-02-23 11:54:22

标签: c# javascript winforms interface comvisible

我有一个WinForm,它通过ObjectForScripting与WebBrowserControl交互。我的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

但现在我的问题。接口包含多个功能。我想为不同的任务分组分离接口。所以我想要一个包含日志功能的接口和一个用于DataAccess功能的接口等等。

所以它会是这样的:

[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

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

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

如何使每个界面的每个功能都可访问?再次,使BaseClass ComVisible并删除ClassInterface属性将起作用,但不是一个选项。

提前致谢!!

1 个答案:

答案 0 :(得分:1)

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

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

希望有所帮助!

相关问题