从 JavaScript 引用 C# 方法而不调用它
我有一个使用 ObjectForScripting 设置的 WebBrowser 控件,这样我就可以从浏览器控件中托管的 JavaScript 调用 C# 方法。
一切似乎都很顺利,除了在我的单元测试中,我对我正在使用的 window.external 方法进行了一些检查,事实证明...
window.external.MyMethod
...似乎工作得与...一样......
window.external.MyMethod()
如果我需要怎么办方法的参考?或者在我的单元测试的情况下:
typeof(window.external.MyMethod) === 'function'
但这会调用该方法(并且失败,因为它返回该方法返回的类型)
如何在不调用该方法的情况下获取对该方法的引用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看看我能找到的文档,它看起来像是指
window.external
的任何成员都会向主机发起调用:我不是特别熟悉这个 API,但似乎您可以执行以下操作:
其中
checkType
是您在 C# 代码中声明的函数,它可以告诉您该类型提供的财产。Looking at what documentation I could find, it looks like referring to any member of
window.external
initiates a call to the host:I'm not specifically familiar with this API, but it seems like you might be able instead to do something like:
Where
checkType
is a function you declare within your C# code that can tell you the type of the property provided.