Can you get a list of variables on the stack in C#

2020-06-06 04:21发布

问题:

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?

回答1:

Yes, the StackFrame class can help you with that.

Linkage

Something along these lines

var currentStackFrame = new StackFrame(1);
var props = currentStackFrame.GetMethod().GetParameters();