.Net – Reflection

Background

When programming tracking the current class/method can be helpful.

Also, capturing the Stack Trace can also be informative.

 

C#

Current Class


this.GetType().FullName

Current Method


System.Reflection.MethodBase.GetCurrentMethod().Name

StackTrace


System.Diagnostics.StackTrace st;
System.Diagnostics.StackFrame [] stFrames;

String strFilename = "";
int    iFileLineNumber = -1;
String strMethodName  = "";

st = new System.Diagnostics.StackTrace(1, true);
stFrames = st.GetFrames();

foreach(System.Diagnostics.StackFrame sf in stFrames )
{

	strMethodName = sf.GetMethod().Name;
	strFilename = sf.GetFileName();
	iFileLineNumber = sf.GetFileLineNumber();

}

Output

 

Vb.Net

Current Class


Me.GetType().FullName

Current Method


System.Reflection.MethodBase.GetCurrentMethod().Name

StackTrace


Dim strFilename As String = ""
Dim iFileLineNumber As Integer = -1
Dim strMethodName as String = ""

Dim st as System.Diagnostics.StackTrace
Dim stFrames() as System.Diagnostics.StackFrame


st = new System.Diagnostics.StackTrace(1, true)
stFrames = st.GetFrames()

For Each sf As System.Diagnostics.StackFrame In stFrames

	strMethodName = sf.GetMethod.Name()
	strFilename = sf.GetFileName()
	iFileLineNumber = sf.GetFileLineNumber()

next

Output

 

Source Code

  1. .Net Fiddle

Summary

This post ended up being a lot of fun.

Comparing the implementation differences between Vb.Net & C#.

Eternal gratitude for those who taught me VB.

And, also the elegance of Anders Hejlsberg.

Turbo Pascal, Delphi, .Net ( C# ), and TypeScript.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s