GetRef to capture methods?

与世无争的帅哥 提交于 2020-02-02 05:19:24

问题


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.


回答1:


No, GetRef doesn't support class methods.




回答2:


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


来源:https://stackoverflow.com/questions/2991637/getref-to-capture-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!