Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Get CallStack parameter values ?

Back to Forum: Tech Off
  • User profile image
    Tief

    Hi,

    I am working on a CustomTraceListener that use Reflection to get trace information such as calling method with parameter info.
    (Code from an example on the Web)

    I can output the name and the parameter name of any method. The problem is, how can i get the runtime values of my method parameters ? 

    I am pretty shure I can't do it with reflection, but i was wondering if there was a class that would enable me to get theses parameter values. 

    The IDE CallTrace window can show us the current runtime method and its parameter values.  How does it do it ?  Can i get theses values ?

    Thx.

    Tief

    using System;
    using System.Diagnostics;
    using System.Text;
    using System.Reflection;

    namespace CustomTraceListener {

    public class CustomTraceListener : TextWriterTraceListener {

    // for our constructors, explicitly call the base class constructor.

    public CustomTraceListener( System.IO.Stream stream, string name ) : base(stream, name) { }

    public CustomTraceListener( System.IO.Stream stream) : base(stream) { }

    public CustomTraceListener( string fileName, string name ) : base(fileName, name) { }

    public CustomTraceListener( string fileName ) : base(fileName) { }

    public CustomTraceListener( System.IO.TextWriter writer, string name ) : base(writer, name) { }

    public CustomTraceListener( System.IO.TextWriter writer ) : base(writer) { }

    public override void Write( string message )

       base.Write( getPreambleMessage() + message );
    }

    public override void WriteLine( string message )
    {
       base.WriteLine( getPreambleMessage() + message );
    }

    [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining )]

    private string getPreambleMessage()
    {
       
    StringBuilder preamble = new StringBuilder();
       
    StackTrace stackTrace = new StackTrace();
       
    StackFrame stackFrame;
       MethodBase stackFrameMethod;
       
       int frameCount = 0;
       
    string typeName;

       do {
          
          frameCount++;
          stackFrame = stackTrace.GetFrame(frameCount);
          stackFrameMethod = stackFrame.GetMethod();
          typeName = stackFrameMethod.ReflectedType.FullName;

       } while ( typeName.StartsWith("System") || typeName.EndsWith("CustomTraceListener") );

    //log DateTime, Namespace, Class and Method Name

    preamble.Append(DateTime.Now.ToString());
    preamble.Append(
    ": ");
    preamble.Append(typeName);
    preamble.Append(
    ".");
    preamble.Append(stackFrameMethod.Name);
    preamble.Append(
    "( ");

    // log parameter types and names

    ParameterInfo[] parameters = stackFrameMethod.GetParameters();

    int parameterIndex = 0;

    while (parameterIndex < parameters.Length)
    {

       preamble.Append(parameters   [parameterIndex].ParameterType.Name);
       preamble.Append(
    " ");
       preamble.Append(parameters[parameterIndex].Name);
       parameterIndex++;

       if (parameterIndex != parameters.Length) preamble.Append(", ");

    }

    preamble.Append(" ): ");

    return preamble.ToString();

    }

    }

    }

  • User profile image
    littleguru
  • User profile image
    footballism

       If I am not mistaken, you cannot do it in managed world, and it really makes sense, if CLR gives you the ability to probe the values pushed on the stack, it will actually opens the door to some unexpected security breach.

       ContextBoundObject can be a solution, but this Object IMHO is useless, given the potential performance implication of CLR's method interception.

       So the best you can do is to reply to some out of band technique such as CLR Profiling APIs.

       This article can get you started with CLR's Profiling APIs:

    No Code Can Hide from the Profiling API in the .NET Framework 2.0

    Sheva

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.