Cecil Has a Name Cecil Has a Name - 1 year ago 180
C# Question

How do I get the executing object for a stackframe?

When using reflection it is possible to obtain the call stack (apart from that it can be a crude approximation due to JIT optimizations) using System.Diagnostics.StackTrace and examine the StackFrame objects contained.

How can I get a reference to the object (the this-pointer) on which a method in a stack frame is executing?

I know I can get the MethodBase by calling GetMethod() on the stack frame object, but what I'm looking for is something along the lines of GetObject() (which'd naturally return null if the method is static). It seems like the stack frame object can only be queried for statically determined info such as method info, originating file etc.

The VS debugger knows (although it probably use another method of obtaining the call stack trace), as one can double click any stack frame in the call stack window and look at the values of the locals and class fields.

To clarify: I want the object instance on which the method was called. I.e.: If method Foo() is called on object instance A somewhere on the call stack, and it cascades to the method I do the stack trace, I'd like to obtain a reference to A from where I perform the stack trace. (Not the declaring type of the method base)

Answer Source

I'm pretty sure that this is not possible. Here's why:

  1. This could break type safety, since anyone can lookup a frame, get the object regardless of which AppDomain\Thread they are executing on or permission they have.

  2. The 'this' (C#) identifier is really just an argument to the instance method (the first), so in reality there is no difference between static methods and instance methods, the compiler does its magic to pass the right this to an instance method, which of course means that you will need to have access to all method arguments to get the this object. (which StackFrame does not support)

It might be possible by using unsafe code to get the pointer of the first argument to an instance method and then casting it to the right type, but I have no knowledge of how to do that, just an idea.

BTW you can imagine instance methods after being compiled to be like C# 3.0 extension methods, they get the this pointer as their first argument.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download