GetRef 来捕获方法?

发布于 2024-09-04 19:08:44 字数 108 浏览 4 评论 0原文

我刚刚发现了 VBScript 的 GetRef 函数,它获取对其参数命名的函数的引用。有没有办法以这种方式获取对方法的引用?我有一种预感,VBScript 不提供这样做所需的复杂绑定,但它肯定会很好。

I've just discovered VBScript's GetRef function, which gets a reference to the function named by its argument. Is there any way to get a reference to a method in this way? I have a hunch that VBScript doesn't offer the sophistication of binding needed to do so, but it would sure be nice.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

对你的占有欲 2024-09-11 19:08:44

不,GetRef 不支持类方法。

No, GetRef doesn't support class methods.

关于从前 2024-09-11 19:08:44

有一个解决方法,请参阅我的答案 这里

这里是完整的示例

Const forReading = 1, forWriting = 2, forAppending = 8, CreateFile = True
Set my_obj = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\temp\test.txt", forWriting, CreateFile)

Function my_function(my_obj, method, text)
  command = "my_obj." & method & " """ & text & """"
  ExecuteGlobal command
End Function

'make a reference to our function
Set proc = GetRef("my_function") 
'and call it with parameters, the first being the method invoked
Call proc(my_obj, "WriteLine", "testing")

'cleanup'
my_obj.Close
Set my_obj = Nothing

There is a workaround for this, see my answer here

Here the full sample

Const forReading = 1, forWriting = 2, forAppending = 8, CreateFile = True
Set my_obj = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\temp\test.txt", forWriting, CreateFile)

Function my_function(my_obj, method, text)
  command = "my_obj." & method & " """ & text & """"
  ExecuteGlobal command
End Function

'make a reference to our function
Set proc = GetRef("my_function") 
'and call it with parameters, the first being the method invoked
Call proc(my_obj, "WriteLine", "testing")

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