MethodBase Without Reflection
One of my friends early this morning (Too early if you ask me. It takes me a lil bit to get my engines going) asked me about getting presently executing method information. Well, that is pretty easy, right? We have System.Reflection that allows us to look into the current codebase for information like that, like getting the methods name as a string.
We just simply do a:
-
System.Reflection.MethodBase.GetCurrentMethod()
Bam, we have the current method name. But, he wanted to do this sans Reflection.
Well, we have to make that one line of code a little bit longer taking into account that constraint, but it really isn’t that complex. We know that for each method that is executed in an arbitrary thread we get one of those StackFrame objects that is pushed onto the base dynamic stack data structure (i.e. We push a new StackFrame object onto the callstack). Those StackFrame objects contain all sorts of fun information, and that information can be extracted and used for whatever scheming purpose you have in mind. But remember, a lot of method information is contained in the debug file, so when you switch to release builds you might elude some data that you are interrogating for.
So firstly, let’s produce a new StackFrame object:
Now we have a StackFrame object, and we know that the StackFrame contains all sorts of MethodBase information that can be employed to garnish information from.
So, let’s do some MethodBase hydrating:
-
MethodBase methodBase = stackFrame.GetMethod();
Now that you got those to ready to go, you can start getting all the fun information!
For example:
-
MethodBase methodBase = stackFrame.GetMethod();
-
string methodName = methodBase.Name;
-
bool isStaticMethod = methodBase.IsStatic;
-
bool isPublicMethod = methodBase.IsPublic;
Ready to go!
3 Comments »
RSS feed for comments on this post. TrackBack URL





















Articles & Research
SharePoint Architecture
Personal/Off-Topic
Latest Free SharePoint Software
SecureCenter For SharePoint
SharePoint Security Assurance Program™
Free Online SharePoint Security Tools
Online SharePoint Security Health Assessment
Article Or Research Filed Under 
“But, he wanted to do this sans Reflection”
So what do we suppose StackFrame is using internally to get this information?
If it’s for “educational purposes” I guess it’s a fair question, but I would choose System.Reflection.MethodBase.GetCurrentMethod()
any day before using StackFrame.
/Jonas
Comment by Jonas — May 7, 2008 @ 1:52 pm
[...] MethodBase Without Reflection – Adam Buenz shows an alternative technique for getting at the methodbase by using stack frames. I question if this is actually reflection free – I suspect stack frame is making use of reflection – but it does illustrate the useful functionality of the stackframe. [...]
Pingback by Reflective Perspective - Chris Alcock » The Morning Brew #89 — May 8, 2008 @ 12:35 am
Thanks! That has helped me greately. I needed to write a piece of code that would show me where the procedure gets run from and I did it by creating a sort of StackTrace helper method.
George
Comment by George C# Developer — December 23, 2008 @ 8:35 am