问题
A .NET application can get a managed StackTrace
that describes which methods were called and holds references to them to get their name, token and signature, and at which IL offset in the method body the call was made. But it does not contain the argument values that were passed to each method.
The argument values are in the process's stack memory somewhere for sure, but that's the native representation which may be a bit unhandy and unpredictable to evaluate.
There is also the managed stack, the one that the CLR essentially executes on, before the JIT compiler. In MSIL code, arguments are put on that stack before the call
opcode is executed. So these values should also be on the CLR stack.
The question is, can a managed application inspect its own managed stack at runtime to extract such information?
I'm not talking about separate debugger processes like Visual Studio. I want to do this from within the process. I also understand that any executed code will add to the stack, so this thing would need to set a certain "entry point" from which I could inspect the stack upwards (i. e. towards to root, if the CLR also keeps stacks hanging from the ceiling...), ignoring what my current method and its called methods do.
回答1:
The reason is described by Raymond Chen here.
The short of it:
A parameter to a method can become eligible for collection while the method is still executing.
It's not intuitive, read the part about JIT and GC working together. So the limitation of the StackTrace info is by design.
来源:https://stackoverflow.com/questions/23959398/inspect-the-managed-stack