79

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

Marek Karbarz
  • 28,956
  • 6
  • 53
  • 73
camainc
  • 3,750
  • 7
  • 35
  • 46
  • Check this out: http://geekswithblogs.net/opiesblog/archive/2006/06/29/83654.aspx – Brian Mains Jan 12 '10 at 18:11
  • That's actually how to get the name of the calling method, not the currently executing method. Still cool, though! – Marc Bollinger Jan 12 '10 at 18:21
  • Possible duplicate of [Can you use reflection to find the name of the currently executing method?](http://stackoverflow.com/questions/44153/can-you-use-reflection-to-find-the-name-of-the-currently-executing-method) – Michael Freidgeim Jan 25 '17 at 22:52

5 Answers5

136

System.Reflection.MethodInfo.GetCurrentMethod();

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
herzmeister
  • 11,101
  • 2
  • 41
  • 51
  • 11
    better use the base class: System.Reflection.MethodBase.GetCurrentMethod(); – marstone Jan 06 '14 at 07:34
  • 2
    @marstone - Could you explain why please? – Gavin Ward Jun 23 '14 at 14:32
  • 7
    @PunkyGuy someone said: “Although both calls have exactly the same effect, it's in general a bad idea to invoke Shared functions on subclasses of the class on which they are actually defined”. reference:http://bytes.com/topic/visual-basic-net/answers/457334-methodinfo-methodbase – marstone Jun 24 '14 at 12:02
  • This does not always work in optimized situations. Sometimes the current method is optimized away. – Maslow Feb 03 '17 at 16:05
47

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
Edward
  • 1,195
  • 11
  • 10
7

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# [<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)

Community
  • 1
  • 1
Maslow
  • 18,464
  • 20
  • 106
  • 193
6

Another approach would be to use Caller​Member​Name​Attribute from the System.​Runtime.​Compiler​Services namespace to populate an optional parameter. For example ...

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.

Frank Boyne
  • 4,400
  • 23
  • 30
2
Dim methodName As String = System.Reflection.MethodBase.GetCurrentMethod().Name
Stephen G Tuggy
  • 991
  • 10
  • 16