Second Life of a Hungarian SharePoint Geek

October 2, 2010

Using call stack information to better understand SharePoint functionality

If you would like to get deeper understanding what happens in SharePoint and why,  probably .NET Reflector is your old friend. It helps you to investigate the code of the internal assemblies and to track call chains by clicking on the method name you would like to follow.

The functionality of Reflector is very nice, but it is sometimes simply more useful to see the calling process in action or check the values of the parameters the methods are called with. That is where call stack comes into the picture.

Recently, I study SharePoint external lists and try to understand the process better to be able to find a way to hook up some extensions to the base framework. In this process I find call stacks extremely useful.

By default, you can see something similar when looking to the Call Stack window in Visual Studio 2010.

image

This information helps you not too much as only your own methods are displayed, external calls are hidden.

Fortunately, you can turn on external code display as well as parameter values, as illustrated on the screenshot below.

image

As you can see there is a Go To Reflector menu either, but I really don’t suggest you to use that due to my bad experiences I wrote about earlier.

After enabling these options, Visual Studio displays the whole call stack, that is really what I needed this time. Now you shouldn’t follow method calls with Reflector step by step, you can check directly the method you think is important.

image

As you can see the parameter values are displayed with the method calls. The bad news is that it seems parameter values are displayed by calling the ToString() method on the parameter. At least I found no way to check the value of parameters having more complex types.

Last but not least you can get (almost) the same result from code. The following snippet shows how to do that using StackTrace, StackFrame, ParameterInfo and MethodBase classes. I wrote almost the same, as you can’t get the values of the parameters this way.

  1. using System;
  2. using System.Text;
  3. using System.Diagnostics;
  4. using System.Reflection;
  5.  
  6. namespace YourNameSpace
  7. {
  8.     public static class Helper
  9.     {
  10.         public static void OutputStackTrace()
  11.         {
  12.             StringBuilder sb = new StringBuilder();
  13.             StackTrace stackTrace = new StackTrace();
  14.             StackFrame[] stackFrames = stackTrace.GetFrames();
  15.             foreach (StackFrame stackFrame in stackFrames)
  16.             {
  17.                 MethodBase mb = stackFrame.GetMethod();
  18.                 sb.AppendFormat("{0}(", mb.Name);
  19.                 ParameterInfo[] pis = mb.GetParameters();
  20.  
  21.                 // aggregating parameters
  22.                 Array.ForEach<ParameterInfo>(pis,
  23.                     new Action<ParameterInfo>(pi => sb.AppendFormat("{0} {1}{2}",
  24.                         pi.ParameterType.Name,
  25.                         pi.Name,
  26.                         (pi.Position != pis.Length – 1) ? ", " : String.Empty)));
  27.  
  28.                 sb.AppendFormat(") – {0}\r\n", mb.DeclaringType.AssemblyQualifiedName);
  29.             }
  30.             String callStack = sb.ToString();
  31.             Debug.WriteLine(callStack);
  32.         }
  33.     }
  34. }

All you have to do is to include a call to this static method where you need the call stack information, and it is dumped to the Debug view of the Output window where you can copy it from for later use.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: