2012-04-19 44 views
1

VBScript中可以確定當前正在執行的函數的名稱嗎?VBScript確定哪個函數正在運行

在.NET中,你可以這樣做:

MethodBase method = MethodBase.GetCurrentMethod(); 
Console.WriteLine(method.Name); 

回答

0

沒有,但你可以很容易地實現它

dim module_name 

sub sub1 
    module_name = "sub1" 
    wscript.echo "i'm " & module_name 
    'do something 
end sub 

function function1 
    module_name = "function1" 
    wscript.echo "i'm " & module_name 
    function1 = "something" 
end function 

在遞歸的情況下,你還記得在使電平找你如果太深,你可以出去。

+0

不是我想要的。我正在尋找一些程序化的東西。 – coson 2012-04-19 23:09:30

+0

我知道,長久以來一直在尋找你自己的麻布,但也不可能,只是現在我只是在Ruby腳本編寫中,我有這樣的奢侈 – peter 2012-04-19 23:26:57

3

過去,我構建了一個callstack查看器來查看每個被調用的函數的性能。這需要一個額外的VBS代碼每個功能/子和一些額外的代碼在運行時的開銷。

底部 - 向上:

Function DoSomething(a, b, c) 
    dim registerFunctionObj : Set registerFunctionObj = [new RegisterFunction]("DoSomething") 

    ' other code 
End Function 

每當函數被調用,它會創建RegisterFunction對象的新實例。當函數退出時,registerFunctionObj變量自動超出範圍,調用實例的Class_Terminate子。

[new RegisterFunction]僅僅是返回一個registerFunction實例的功能:

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類的單一實例,但你可以把它你的項目的一部分,讓你找回通過你的全球項目類:

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 

聲明:這裏的代碼是純粹的演示代碼在飛行中創建。它缺乏功能,只是在這裏解釋這個概念。它可能包含錯誤並且不完整。

您唯一需要的是每個功能的一行代碼和您自己的想象力來擴展它。

+0

這很有趣,我必須檢查一下... – coson 2012-04-20 16:34:05

相關問題