2011-02-23 18 views
3

我有一個WinForm,它通過ObjectForScripting與WebBrowserControl交互。我的WinForm的基類不ComVisible,我不能或不會改變它。因爲有一個NonComVisibleBaseClass我創建了一個接口並將其設置爲ComVisible(true)並設置了FormAttribute [ClassInterface(ClassInterfaceType.None)]。接口中的方法可以由JavaScript調用。它的作品完美:ObjectForScripting與多個接口不工作

//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 

但現在我的問題。該界面包含多個功能。我想爲單獨的任務分組分隔接口。所以我想要一個包含Logging函數和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

在做類似的項目時,我們發現JavaScript只能訪問生成COM包裝器的默認接口,在你選擇第一個ComVisible接口的情況下,它會找到默認的接口,因爲你沒有明確地設置默認的接口屬性。問題是JavaScript沒有QueryInterface類似物。

爲了訪問其他接口,我們需要爲JavaScript創建我們自己的QueryInterface版本,可以通過在默認界面中提供顯式的轉換類型函數(不是那麼優雅)或者具有可以執行轉換爲正確的ComVisible接口類型。

希望有幫助!