kamagra how much to take

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:

  1. 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:

  1. StackFrame stackFrame = new StackFrame();

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:

  1. StackFrame stackFrame = new StackFrame();
  2. MethodBase methodBase = stackFrame.GetMethod();

Now that you got those to ready to go, you can start getting all the fun information!
For example:

  1. StackFrame stackFrame = new StackFrame();
  2. MethodBase methodBase = stackFrame.GetMethod();
  3. string methodName = methodBase.Name;
  4. bool isStaticMethod = methodBase.IsStatic;
  5. bool isPublicMethod = methodBase.IsPublic;

Ready to go! :)



  1. Jonas says:

    “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.


  2. George C# Developer says:

    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.


  1. Reflective Perspective - Chris Alcock » The Morning Brew #89 - [...] MethodBase Without Reflection - Adam Buenz shows an alternative technique for getting at the methodbase by using stack frames. …

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>