About      |       Articles      |      Services      |      Software      |      Contact

Latest Free SharePoint Software

ARB Security Solutions regularly releases free SharePoint software, including WebParts, Client Applications, Framework Extensions, and other Miscellaneous Components.
The most recent freeware is:

Title: Simple SharePoint Rollup WebPart
Date Published: 10/22/2009

Previous Two Free WebPart Releases:

SecureCenter For SharePoint

By SharePoint security integrators, for SharePoint security integrators.

SharePoint Security Assurance Program™

For externally facing SharePoint deployments, security is an acutely important deployment concern. Learn how through daily security scanning, you can ensure external business users and partners that they can collaborate in confidence!

Security Assurance WebPart:



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:

C#:
  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:

C#:
  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:

C#:
  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:

C#:
  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! :)

  • Share/Bookmark

3 Comments »

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

  2. [...] 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

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

RSS feed for comments on this post. TrackBack URL

Leave a comment