This is kind of a silly question, but is it possible to get the name of the method that is currently being executed from within that method?
这是一个愚蠢的问题,但是有可能从该方法中获取当前正在执行的方法的名称吗?
Public Sub SomeMethod()
Dim methodName as String = System.Reflection.[function to get the current method name here?]
End Sub
Thanks
谢谢
5 个解决方案
#1
104
System.Reflection.MethodInfo.GetCurrentMethod();
System.Reflection.MethodInfo.GetCurrentMethod();
#2
32
The other methods are close to what was asked, but they don't return the string value. But this does:
其他方法接近所要求的方法,但它们不返回字符串值。但这样做:
Dim methodName$ = System.Reflection.MethodBase.GetCurrentMethod().Name
#3
4
Another approach would be to use CallerMemberNameAttribute from the System.Runtime.CompilerServices namespace to populate an optional parameter. For example ...
另一种方法是使用System.Runtime.Compiler Services命名空间中的Caller Member Name属性来填充可选参数。例如 ...
Private Function GetMethodName(<System.Runtime.CompilerServices.CallerMemberName>
Optional memberName As String = Nothing) As String
Return memberName
End Function
The function would be invoked as you would expect...
该函数将按您的预期调用...
Public Sub DoSomeWork()
Dim methodName As String = GetMethodName()
Console.WriteLine($"Entered {methodName}")
' Do some work
End Sub
Rather than 'just' retrieving the method name, the function might also make use of the method name retrieved to further simplify code. For example...
该函数不是“只是”检索方法名称,而是还可以利用检索到的方法名称来进一步简化代码。例如...
Private Sub TraceEnter(
<System.Runtime.CompilerServices.CallerMemberName>
Optional memberName As String = Nothing)
Console.WriteLine($"Entered {memberName}")
End Sub
... which might be used like this ...
...可能会像这样使用......
Public Sub DoSomeWork()
TraceEnter()
' Do some work
End Sub
Other attributes in the CompilerServices namespace may be used in similar fashion to retrieve the full path (at compile time) of the source file and/or the line number of the call. See the CallerMemberNameAttribute documentation for sample code.
可以以类似的方式使用CompilerServices名称空间中的其他属性来检索源文件的完整路径(在编译时)和/或调用的行号。有关示例代码,请参阅CallerMemberNameAttribute文档。
#4
3
To guarantee that any of the answers presented to this question actually work (System.Reflection.MethodBase.GetCurrentMethod().Name
) at runtime, you need to add an attribute. There are no compiler/runtime flags that I know of that break this method:
为了保证在运行时实际提供给此问题的任何答案(System.Reflection.MethodBase.GetCurrentMethod()。Name),您需要添加一个属性。我知道没有编译器/运行时标志可以打破这个方法:
the function you are trying to get the name of must be marked
必须标记您要获取名称的功能
- F#
[<System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>]
-
F#[
] (system.runtime.compilerservices.methodimploptions.noinlining)> -
VB:
<System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>
VB:
(system.runtime.compilerservices.methodimploptions.noinlining)> -
C#:
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
C#:[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
Also, nowadays, there is the nameof()
operator in VB, C#(and maybe F# soon) which for your case would be nameof(SomeMethod)
(I believe the syntax would be the same for VB and C# here)
此外,如今,VB中有nameof()运算符,C#(可能很快就是F#),对于你的情况就是nameof(SomeMethod)(我相信VB和C#的语法相同)
#5
1
Dim methodName As String = System.Reflection.MethodBase.GetCurrentMethod().Name
#1
104
System.Reflection.MethodInfo.GetCurrentMethod();
System.Reflection.MethodInfo.GetCurrentMethod();
#2
32
The other methods are close to what was asked, but they don't return the string value. But this does:
其他方法接近所要求的方法,但它们不返回字符串值。但这样做:
Dim methodName$ = System.Reflection.MethodBase.GetCurrentMethod().Name
#3
4
Another approach would be to use CallerMemberNameAttribute from the System.Runtime.CompilerServices namespace to populate an optional parameter. For example ...
另一种方法是使用System.Runtime.Compiler Services命名空间中的Caller Member Name属性来填充可选参数。例如 ...
Private Function GetMethodName(<System.Runtime.CompilerServices.CallerMemberName>
Optional memberName As String = Nothing) As String
Return memberName
End Function
The function would be invoked as you would expect...
该函数将按您的预期调用...
Public Sub DoSomeWork()
Dim methodName As String = GetMethodName()
Console.WriteLine($"Entered {methodName}")
' Do some work
End Sub
Rather than 'just' retrieving the method name, the function might also make use of the method name retrieved to further simplify code. For example...
该函数不是“只是”检索方法名称,而是还可以利用检索到的方法名称来进一步简化代码。例如...
Private Sub TraceEnter(
<System.Runtime.CompilerServices.CallerMemberName>
Optional memberName As String = Nothing)
Console.WriteLine($"Entered {memberName}")
End Sub
... which might be used like this ...
...可能会像这样使用......
Public Sub DoSomeWork()
TraceEnter()
' Do some work
End Sub
Other attributes in the CompilerServices namespace may be used in similar fashion to retrieve the full path (at compile time) of the source file and/or the line number of the call. See the CallerMemberNameAttribute documentation for sample code.
可以以类似的方式使用CompilerServices名称空间中的其他属性来检索源文件的完整路径(在编译时)和/或调用的行号。有关示例代码,请参阅CallerMemberNameAttribute文档。
#4
3
To guarantee that any of the answers presented to this question actually work (System.Reflection.MethodBase.GetCurrentMethod().Name
) at runtime, you need to add an attribute. There are no compiler/runtime flags that I know of that break this method:
为了保证在运行时实际提供给此问题的任何答案(System.Reflection.MethodBase.GetCurrentMethod()。Name),您需要添加一个属性。我知道没有编译器/运行时标志可以打破这个方法:
the function you are trying to get the name of must be marked
必须标记您要获取名称的功能
- F#
[<System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>]
-
F#[
] (system.runtime.compilerservices.methodimploptions.noinlining)> -
VB:
<System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>
VB:
(system.runtime.compilerservices.methodimploptions.noinlining)> -
C#:
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
C#:[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
Also, nowadays, there is the nameof()
operator in VB, C#(and maybe F# soon) which for your case would be nameof(SomeMethod)
(I believe the syntax would be the same for VB and C# here)
此外,如今,VB中有nameof()运算符,C#(可能很快就是F#),对于你的情况就是nameof(SomeMethod)(我相信VB和C#的语法相同)
#5
1
Dim methodName As String = System.Reflection.MethodBase.GetCurrentMethod().Name