Retrieving The Name of a Method

There might be times when you need the name of the method when logging Exceptions etc. The code below uses reflection and will also add the class name:

    1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    2       Dim dir = String.Format(“{0}.{1}”, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name, System.Reflection.MethodBase.GetCurrentMethod().Name)

The output would look like this:

>? dir
"Form1.Form1_Load"

If you also want the name of the application, use DeclaringType.FullName instead of just .Name.

Tip Submitted By: David McCarter

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s