开发者

Refer to C# method from JavaScript without calling it

开发者 https://www.devze.com 2023-04-13 02:26 出处:网络
I have a WebBrowser control set up with ObjectForScripting such that I can call C# methods from JavaScript hosted in the browser control.

I have a WebBrowser control set up with ObjectForScripting such that I can call C# methods from JavaScript hosted in the browser control.

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.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 g开发者_Python百科et 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:

...a reference to "window.external.speech" will call the host to resolve the name "speech."

I'm not specifically familiar with this API, but it seems like you might be able instead to do something like:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号