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.
EDIT: 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)
I'm pretty sure that this is not possible. Here's why:
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.
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.
It's possible to obtain reference to thiscall
object, but not with .NET code only. Native code must be involved. Even with using dynamic classes and System.Relection.Emit namespace .NET does not have instruments to access another methods evaluation stack and arguments.
On other side if you disassemble your .NET method you can see that this reference does not passed on physical stack at all. Thiscall
reference is stored in ECX
(RCX
for x64) register instead. So it is possible climb up on stack from your method to method from which you want to obtain thiscall
object. And then lookup inside that method's machine codes for instruction which save register ECX (RCX) in stack, and get from that instruction relative address where that reference lies.
Of course, the method of climbing is severely different in x32 and x64 application. To produce such function you must use not only C# but assembly code, and keep in mind that inline assembler is not allowed under x64; it must be a full assembler module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With