获取当前方法的名称
这是一个愚蠢的问题,但是有可能从该方法中获取当前正在执行的方法的名称?
Public Sub SomeMethod() Dim methodName as String = System.Reflection.[function to get the current method name here?] End Sub 谢谢
 System.Reflection.MethodInfo.GetCurrentMethod(); 
其他方法接近要求,但不返回string值。 但是这样做:
 Dim methodName$ = System.Reflection.MethodBase.GetCurrentMethod().Name 
 为了保证在这个问题中出现的任何答案在运行时都能正常工作( System.Reflection.MethodBase.GetCurrentMethod().Name ),你需要添加一个属性。 没有编译器/运行时标志,我知道这个打破了这个方法: 
你试图得到的名字的function必须被标记
-   F# [<System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>]
- 
VB: <System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>
- 
C#: [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
 另外,现在在VB,C#( 也许是F# )中有nameof()运算符,对于你的情况,这将是nameof(SomeMethod) (我相信这里的VB和C#的语法是相同的) 
 Dim methodName As String = System.Reflection.MethodBase.GetCurrentMethod().Name 
另一种方法是使用System.Runtime.CompilerServices命名空间中的Caller Member Name Attribute来填充可选参数。 例如 …
 Private Function GetMethodName(<System.Runtime.CompilerServices.CallerMemberName> Optional memberName As String = Nothing) As String Return memberName End Function 
该函数将被调用,因为你期望…
 Public Sub DoSomeWork() Dim methodName As String = GetMethodName() Console.WriteLine($"Entered {methodName}") ' Do some work End Sub 
函数也可以使用检索的方法名称来进一步简化代码,而不是“检索”方法名称。 例如…
 Private Sub TraceEnter( <System.Runtime.CompilerServices.CallerMemberName> Optional memberName As String = Nothing) Console.WriteLine($"Entered {memberName}") End Sub 
…可能会像这样使用…
 Public Sub DoSomeWork() TraceEnter() ' Do some work End Sub 
CompilerServices名称空间中的其他属性可以以类似的方式使用,以检索源文件的完整path(在编译时)和/或调用的行号。 有关示例代码,请参阅CallerMemberNameAttribute文档。