问题描述
限时送ChatGPT账号..是否可以在 VBScript 中确定当前正在执行的函数的名称?
Is it possible in VBScript to determine the name of the function currently executing?
在 .NET 中,您可以:
In .NET, you could do:
MethodBase method = MethodBase.GetCurrentMethod();
Console.WriteLine(method.Name);
推荐答案
过去,我构建了一个调用堆栈查看器来查看被调用的每个函数的性能.由于额外的代码,这当然需要每个函数/子额外一行 VBS 代码以及运行时的一些开销.
In the past, I build a callstack viewer to see the performance of each function that is called. This needs one extra line of VBS code per function/sub and some overhead during runtime of course because of the extra code.
自下而上:
Function DoSomething(a, b, c)
dim registerFunctionObj : Set registerFunctionObj = [new RegisterFunction]("DoSomething")
' other code
End Function
无论何时调用该函数,它都会创建一个 RegisterFunction 对象的新实例.当函数退出时,registerFunctionObj
变量自动超出范围,调用实例的 Class_Terminate 子.
Whenever the function is called, it creates a new instance of the RegisterFunction object. When the function exits, the registerFunctionObj
variable goes out of scope automatically, calling the Class_Terminate sub of the instance.
[new RegisterFunction]
只是一个返回 registerFunction 实例的函数:
[new RegisterFunction]
is just a function that return a registerFunction instance:
Function [new RegisterFunction](funcName)
Set [new RegisterFunction] = new cls_RegisterFunction
[new RegisterFunction].FunctionName = funcName
Set [new RegisterFunction].CallStackViewer = CallStackViewer
End function
Class cls_RegisterFunction
Private functionName_, startTime_, callStackViewer_, endTime_
Private Sub Class_Initialize
startTime_ = now
callStackViewer_.LogInitialize me
End Sub
Public Property Let FunctionName(fName)
functionName_ = fName
End Property
Public Property Set CallStackViewer(byRef csv)
Set callStackViewer_ = csv
End Property
Private Sub Class_Terminate
endTime_ = now
callStackViewer_.LogTerminate me
End Sub
End Class
CallStackViewer 实例是 CallStackViewer 类的单例实例,但您可以将其作为项目的一部分,以便通过全局项目类检索它:
The CallStackViewer instance is a singleton instance of the a CallStackViewer class, but you can make it a part of your project, so you retrieve it through you global project class:
Private PRIV_callStackViewer
Public Function CallStackViewer()
If not IsObject(PRIV_callStackViewer) Then
Set PRIV_callStackViewer = new cls_CallStackViewer
End If
Set CallStackViewer = PRIV_callStackViewer
End Function
Class cls_CallStackViewer
Public Sub Class_Initialize
' Here you can retrieve all function libraries (as text file) extract the
' function name, the file they are in and the linenumber
' Put them in a dictionary or a custom object
End Sub
Public Sub LogInitialize(byref registerFunction)
' Here you can push the function on a stack (use a standard dotnet list>stack for it),
' log the starttime to a log object or handle custom breakpoints
End Sub
Public Sub LogTerminate(byref registerFunction)
' Here you can pop the function from a stack, log the endtime to a log
' object or handle custom breakpoints
End Sub
End Class
免责声明:此处的代码是即时创建的纯演示代码.它缺乏功能,只是在这里解释这个概念.它可能包含错误并且不完整.
Disclaimer: The code in here is pure demo code created on the fly. It lacks functionality and is only here to explain the concept. It could contain errors and is not complete.
您唯一需要的是每个函数一行代码以及您自己的想象力来扩展它.
The only thing you need is one line of code per function and your own imagination to expand it.
这篇关于VBScript 判断正在运行的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论