简体   繁体   English

从JavaScript引用C#方法而不调用它

[英]Refer to C# method from JavaScript without calling it

I have a WebBrowser control set up with ObjectForScripting such that I can call C# methods from JavaScript hosted in the browser control. 我有一个使用ObjectForScripting设置的WebBrowser控件,这样我就可以从浏览器控件中托管的JavaScript调用C#方法。

Everything seems to be going fine except that in my unit tests I have some checks on the window.external methods I am using and it turns out that... 除了在单元测试中我对window.external方法进行了一些检查外,其他一切似乎都很好,事实证明……

window.external.MyMethod

...seems to work just the same as... 似乎与...相同

window.external.MyMethod()

What if I need a reference to the method? 如果需要参考该方法怎么办? Or in the case of my unit test: 或就我的单元测试而言:

typeof(window.external.MyMethod) === 'function'

But this calls the method (and fails because it returns the type of whatever was returned by the method) 但这会调用该方法(并且失败,因为它返回该方法返回的值的类型)

How do I get a reference to the method without calling it? 如何在不调用方法的情况下获得对方法的引用?

Looking at what documentation I could find, it looks like referring to any member of window.external initiates a call to the host: 看一下我能找到的文档 ,看起来就像引用window.external任何成员一样,都发起了对主机的调用:

...a reference to "window.external.speech" will call the host to resolve the name "speech." ...对“ window.external.speech”的引用将调用主机来解析名称“ speech”。

I'm not specifically familiar with this API, but it seems like you might be able instead to do something like: 我对此API并不特别熟悉,但似乎您可以执行以下操作:

window.external.checkType('MyMethod') === 'function'

Where checkType is a function you declare within your C# code that can tell you the type of the property provided. 其中checkType是您在C#代码中声明的函数,可以告诉您所提供属性的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM