Get the name of the current method

后端 未结 5 924
野的像风
野的像风 2021-02-06 20:05

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 SomeMetho         


        
5条回答
  •  遥遥无期
    2021-02-06 20:56

    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:

    the function you are trying to get the name of must be marked

    • F# []
    • VB:

    • 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)

提交回复
热议问题