Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the values of the parameters up the stack trace

Printing the stack trace is not so difficult when using System.Diagnostics. I am wondering if it is possible to print the VALUES of the parameters passed to each method up the stack trace, and if not why not.

Here is my preliminary code:

public static class CallStackTracker
{
    public static void Print()
    {
        var st = new StackTrace();
        for (int i = 0; i < st.FrameCount; i++)
        {
            var frame = st.GetFrame(i);
            var mb = frame.GetMethod();
            var parameters = mb.GetParameters();
            foreach (var p in parameters)
            {
                // Stuff probably goes here, but is there another way?
            }
        }
    }
}

Thanks in advance.

like image 309
user420667 Avatar asked Apr 11 '12 21:04

user420667


1 Answers

You can't do that, at least not with the classes provided by System.Diagnostics. The StackFrame class doesn't provide a way to access the argument values (MethodBase.GetParameters provides information about the declared parameters, e.g. their names and types, but not the values of the actual arguments)

I think it's possible to do it with the CLR debugging APIs, but probably not from C#

like image 86
Thomas Levesque Avatar answered Nov 01 '22 17:11

Thomas Levesque