0
我有一個函數將Func作爲參數,調用它並輸出一些結果。出於記錄目的,能夠訪問lambda表達式執行時實際被調用的函數的名稱是很好的。有沒有辦法在緊湊的框架中做到這一點?獲取作爲參數傳遞的委託的方法名稱(精簡框架)
Private Function tryWithLogging(ByVal moveFunc As Func(Of Boolean)) As Boolean
Try
moveFunc.Invoke()
Dim nameOfMethod as String = '??????
Console.WriteLine("Invoked " & nameOfMethod)
Catch ex As Exception
End Try
End Function
用法示例:
tryWithLogging(Function() myFunction([arbitrary params])
'desired output: "Invoked myFunction"
這裏就是我試圖檢索名稱:
moveFunc.Method.Name 'returns whatever the compiler decided to name the lambda
New StackTrace().GetFrame(1).GetMethod().Name 'works, but is not available on CF
我也試圖傳遞一個Expression(Of Func(Of Boolean))
來代替,而從獲得的名字,但CF上也沒有System.Linq.Expressions
。
是否有任何方法來檢索由緊湊框架3.5的束縛內lambda調用函數的名稱?
像這樣的東西? https://stackoverflow.com/questions/12098267/how-can-i-get-the-name-of-the-calling-function-in-c?noredirect=1&lq=1 –
@LB也許我誤解了你的鏈接,但我不認爲'CallerMemberName'會幫助我。我想要在lambda中調用方法_being_的名稱,而不是調用的函數的名稱。 – Cobalt