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

发布于 2024-12-10 07:37:40 字数 473 浏览 6 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑轻吟梦一曲 2024-12-17 07:37:40

看看我能找到的文档,它看起来像是指window.external 的任何成员都会向主机发起调用:

...对“window.external.speech”的引用将调用主机来解析名称“speech”。

我不是特别熟悉这个 API,但似乎您可以执行以下操作:

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文