全局函数是否有等效的 CallByName? [英] Is there a CallByName equivalent for global functions?
问题描述
我想这样做:如果有一个模块 X 包含一个函数 Y 则调用它,否则不要."
I want to do this: "If there's a module X containing a function Y then call it, otherwise don't."
我知道我可以使用 CallByName(Object, MethodName, ...)
来调用对象实例的方法或属性.
I'm aware that I can use CallByName(Object, MethodName, ...)
to call a method or property of an object instance.
是否可以调用未绑定到对象的全局子/函数?
Is it possible to call a global sub/function which is not bound to an object?
//Module1
Public Sub DoSomething
End Sub
//Module2
Public Sub TriggerDoSomething
On Error Resume Next
CallByName2 "Module1", "DoSomething", ...
End Sub
我知道,最好重构我的代码以将 DoSomething
包装到一个类中,但目前这是不可能的,因为它会以一种不可能的方式破坏我同事的代码几个小时后修复.
I know, it would be better to refactor my code to wrap DoSomething
into a class, but currently this is not possible as it would break the code of my coworkers in a way that could not be fixed in a few hours.
推荐答案
CallByName 真的会更容易一个类 - 难道你不能只用一个将调用重定向到模块的类包装模块吗?
It really would be easier to CallByName a class - can't you just wrap the module(s) with a class that redirects the calls to the module?
可以使用 FunctionDelegator 按名称调用模块中的例程.这在 Matt Curland 的优秀著作 Advanced Visual Basic 6一>.
It's possible to call routines in modules by name using a FunctionDelegator. This is explained in Matt Curland's excellent book Advanced Visual Basic 6.
谷歌搜索可能会发现一些使用 CallWindowProc 的技巧,但 Matt Curland 说这很危险.这几乎是对此事的最终决定:)
Googling might find you some hacks using CallWindowProc, but Matt Curland says that's dangerous. Which is pretty much the final word on the matter :)
RS Conley 的答案不使用模块,使用具有 GlobalMultiUse 实例化属性的类,您将能够使用 CallByName.如果您的代码在 DLL 而不是 EXE 中,这将起作用.RS Conley 建议在任何情况下,在 DLL 中拥有一个包含几乎所有功能的最小 EXE 会更灵活:这很可能是真的.
RS Conley's answer doesn't use a module, use a class that has a instancing property of GlobalMultiUse and you will be able to use CallByName. This will work if your code is in a DLL rather than an EXE. RS Conley suggests in any case it is more flexible to have a minimal EXE with nearly all functionality in a DLL: this may well be true.
这篇关于全局函数是否有等效的 CallByName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!