2017-08-28 60 views
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调用函数的名称?

+0

像这样的东西? https://stackoverflow.com/questions/12098267/how-can-i-get-the-name-of-the-calling-function-in-c?noredirect=1&lq=1 –

+0

@LB也许我误解了你的链接,但我不认为'CallerMemberName'会帮助我。我想要在lambda中调用方法_being_的名称,而不是调用的函数的名称。 – Cobalt

回答

0

不幸的是,没有办法得到它(你不能得到调用者的名字,抛出异常和手动解析堆栈跟踪)。记住你使用的CF是十年前的。即使那样,它也没有完整框架的所有细节。