Home > Technology > Reflection within a method

Reflection within a method

November 24th, 2009 Leave a comment Go to comments

Who am I?
Where am I?

Have you ever asked that question while examining your code? Before I learned about the Call Stack in Visual Studio 2010, I faced that problem often. I’d have a million of these little debugging statements scattered throughout my code.

string callingAssemblyName = System.Reflection.Assembly.GetCallingAssembly().FullName;
string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;
string className = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString();

Usage:

using System.Diagnostics;

Debug.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "() called.");

or

string methodName = System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name;
Debug.WriteLine(methodName + "(" + <method args> + ") called.");
...
Debug.WriteLine(methodName + "() ending.");
Tags: ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.