0

I have a requirement where in on each step of code execution, I want to access Locals window variables programatically in C#/Visual Studio. Is there a way to do this ?

Is there a particular command to get those variables just like we have something for checking the call stack "Environment.StackTrace" ?

Locals Window in Visual Studio image

While debugging we can see all variables of current scope in Locals window -> So I wanted to write a piece of code which on each line tells me the status of current locals. Example:

void function()
{
    var x = 0, y = 0;
    printLocals()
    x = 1;
    printLocals()
    y = 1;
    printLocals()
}

So here, printLocals() method is a placeHolder which should give me all local variables, just as they are getting displayed in locals window

Jack Zhai
  • 6,230
  • 1
  • 12
  • 20
Rajat Doshi
  • 118
  • 7
  • `I want to access Locals window variables programatically in C#/Visual Studio.` What's that? – Nikhil Agrawal Apr 29 '17 at 07:28
  • If Visual Studio finds them that means they are somewhere in the code. Go fish. – Visual Vincent Apr 29 '17 at 08:04
  • @Rajat Doshi, we could watch the values of variables while Debugging with about 7 ways like this document: https://blogs.msdn.microsoft.com/visualstudioalm/2016/07/15/7-ways-to-look-at-the-values-of-variables-while-debugging-in-visual-studio/, but I'm not very sure that how you want to achieve it in C#, whether there is a way which is what you want to get in that blog? – Jack Zhai May 01 '17 at 07:15
  • @JackZhai-MSFT - Thanks for the reply. My requirement is a bit different then the solutions mentioned in the code. Let me rephrase it with an example. Please see above edits – Rajat Doshi May 02 '17 at 06:53
  • @Rajat Doshi, If you want to visit the different local value, why not debug your app using the "step Into(F11)", so you could visit the local window and view the value during debugging one step by one step. If you just want to print the variables value, how about using the Expression API? http://stackoverflow.com/questions/729803/print-name-of-the-variable-in-c-sharp. But it will require you to add the code to every line. Actually I feel that using the default debugging feature would be better than this way, like we could visit the value in the Immediate Window with or local window directly. – Jack Zhai May 02 '17 at 08:43
  • @JackZhai-MSFT- My requirement is bit unique. So I have a script which adds printLocal() method at the beginning & end of each function(just before return statement), and at those places I want to get status of all locals available in the executing scope. And I want this locals data to be dumped into a file. And hence wanted some code snippet to achieve the same. The suggestion of expression API is good but cannot use this as this needs variable/object names to be supplied to it for further operations. Any further help would be greatly appreciated. Thanks !! – Rajat Doshi May 03 '17 at 03:40
  • @Rajat Doshi, Sorry for that I also have no good suggestion, like my previous suggestion, we often use the immediate like this case: http://stackoverflow.com/questions/1333317/output-a-watched-visual-studio-variable-to-a-file. If it is not the workaround you want to get, I will keep open this case for you, maybe other developing members have other good suggestions. – Jack Zhai May 03 '17 at 09:26
  • Possible duplicate of [How to get a dump of all local variables?](http://stackoverflow.com/questions/9788962/how-to-get-a-dump-of-all-local-variables) – RB. May 03 '17 at 09:29
  • Also duplicate of http://stackoverflow.com/questions/5929384/c-sharp-reusable-function-to-dump-current-value-of-local-variables. – RB. May 03 '17 at 09:29

0 Answers0