简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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