.net 获取当前方法的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2051217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the name of the current method
提问by camainc
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
谢谢
回答by herzmeister
回答by Edward
The other methods are close to what was asked, but they don't return the stringvalue. But this does:
其他方法与所要求的接近,但它们不返回字符串值。但这确实:
Dim methodName$ = System.Reflection.MethodBase.GetCurrentMethod().Name
回答by Maslow
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)>] VB:
<System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)>C#:
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
- 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)]
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)
此外,现在,nameof()在 VB、C#(也许很快就会有 F#)中有运算符,对于您的情况来说是这样的nameof(SomeMethod)(我相信这里的 VB 和 C# 的语法是相同的)
回答by Frank Boyne
Another approach would be to use Caller?Member?Name?Attributefrom the System.?Runtime.?Compiler?Services namespace to populate an optional parameter. For example ...
另一种方法是使用System.?Runtime.?Compiler?Services 命名空间中的Caller?Member?Name?Attribute来填充可选参数。例如 ...
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 文档。
回答by Stephen G Tuggy
Dim methodName As String = System.Reflection.MethodBase.GetCurrentMethod().Name

