All, just wondering if it's possible in .NET/C# to get a list of variables on the stack and their values? I am creating an exception handler for my app and beyond a standard stack trace I'd also like to see the names and values for any variables that are on the stack. Any idea if this can be done?
Asked
Active
Viewed 5,053 times
1 Answers
3
Yes, the StackFrame class can help you with that.
Something along these lines
var currentStackFrame = new StackFrame(1);
var props = currentStackFrame.GetMethod().GetParameters();

BFree
- 102,548
- 21
- 159
- 201
-
1This will only get the names of the parameters, not the values, and tells you nothing about the other variables on the stack. – Daniel Earwicker Dec 17 '08 at 22:45
-
@Jimmy I find if you just link to an article, stack overflow will become: One, Like every other forum on the net where everyone says RTFM and Two its not really helping anyone because most of the time people will come here and ask for code because they have a hard time understanding it. – Nathan W Dec 17 '08 at 22:47
-
Yeah with what earwicker said, I don't think this allows you to get any values (although you can get other variables on the stack by looping through each frame on the StackTrace object). Any ideas on how to get the values? – Dec 17 '08 at 23:32
-
@Earwicker/Alphamacaroon: Could you not use reflection to then extend that sentiment and grab the variable values for each of the variables in the stack? – BenAlabaster Dec 18 '08 at 00:54
-
You pretty much need an object instance to get any values using reflection. Not sure where we would get an object instance from the StackFrame or StackTrace method. – Dec 18 '08 at 01:35
-
I know this should be possible because applications like RedAnt profiler enable you to see variables and their values... – Dec 18 '08 at 01:36
-
"extend that sentiment" - you mean close my eyes and make a wish? – Daniel Earwicker Dec 18 '08 at 17:02