Getting debugger context in C# interactive
Asked Answered
T

3

49

C# Interactive seems a lot more powerful than the Immediate Window (at least it handles lambda expressions that are often used in LINQ - see Visual Studio debugging "quick watch" tool and lambda expressions), but it looks like it can't be used as a replacement as it doesn't know about the debugger context. Is there a way to have access to the debugger context?

I've seen Getting debugger context in F# interactive and it might require the same things, but maybe there are new/different things available.

http://extendedimmediatewin.codeplex.com/ could be useful but looks quite dead, although someone said on Oct 18 2011 that he was planning to port it to VB.Net ( http://extendedimmediatewin.codeplex.com/discussions/75589 ).

Tarahtaran answered 26/10, 2011 at 2:28 Comment(1)
Great question! This is the most important application of Roslyn IMO.Caudad
E
51

Disclaimer: I work for Microsoft on the Roslyn team.

Unfortunately, we don't have a way to get the debugger context right now. We are aware that it would useful, and as we get more of the core deliverables for Roslyn completed we hope to investigate more and see what can be done.

Electronarcosis answered 26/10, 2011 at 3:20 Comment(7)
Good to know that it may happen someday. Thanks for the insider comment.Tarahtaran
Years later, has this ever been addressed?Staggs
Web, Rails, all things have interactive console. I'm surprised this isn't adopted across all platforms.Archean
C# could be a greater programming language in the modern era like Python and many others, if the team were more interested in the state-of-the-art use/features of programming. So many years later, in order to debug a simple small piece of code, I still need to create a whole console project. That is one reason why people in the field of AI, machine learning, data mining are not choosing C# as the primary language.Constellation
Yep going for python next projectPantywaist
It's been 10 year after this post and still no improvement on C# Interactive, no wonder Microsoft is dying.Exemplificative
@KevinPilch what's the status on this? Is there an issue we can track? I checked in the Git repo but couldn't find much.Excitation
H
52

Not a complete solution, but in VS2015, after you've run Initialize Interactive with Project, you can use Debug > Attach to Process to connect to the InteractiveHost.exe process.

You can then debug functions from your project. You won't be able to debug one-off functions in the Interactive window, though, as Kevin explained.

As as side note, https://mcmap.net/q/126320/-can-the-c-interactive-window-interact-with-my-code points out that you may have to have a C# Interactive window open before you open your solution in order to successfully Initialize Interactive with Project

Helvetic answered 30/3, 2017 at 1:29 Comment(4)
Just did this in VS2017 and it's amazing. Kudos!Aquifer
Works in 2019 too. BTW if you "lose" the interactive window after VS switches to debug mode - just go to View - Other windows - C# InteractiveFizzle
DAMN !!! Attach to process is an exceptional feature I didn't know for past 10 years!! Now I can control any program on Windows with C# Interactive !!!Exemplificative
I don't see this option on my Visual Studio 2022 installationExcitation
E
51

Disclaimer: I work for Microsoft on the Roslyn team.

Unfortunately, we don't have a way to get the debugger context right now. We are aware that it would useful, and as we get more of the core deliverables for Roslyn completed we hope to investigate more and see what can be done.

Electronarcosis answered 26/10, 2011 at 3:20 Comment(7)
Good to know that it may happen someday. Thanks for the insider comment.Tarahtaran
Years later, has this ever been addressed?Staggs
Web, Rails, all things have interactive console. I'm surprised this isn't adopted across all platforms.Archean
C# could be a greater programming language in the modern era like Python and many others, if the team were more interested in the state-of-the-art use/features of programming. So many years later, in order to debug a simple small piece of code, I still need to create a whole console project. That is one reason why people in the field of AI, machine learning, data mining are not choosing C# as the primary language.Constellation
Yep going for python next projectPantywaist
It's been 10 year after this post and still no improvement on C# Interactive, no wonder Microsoft is dying.Exemplificative
@KevinPilch what's the status on this? Is there an issue we can track? I checked in the Git repo but couldn't find much.Excitation
T
3

Inside C# Interactive you can run

System.Diagnostics.Debugger.Launch();

to request a debuger, whereby you will get a window asking you to choose one of the open Visual Studio instances or a new instance to attach. Assuming you used "Load Interactive with the Project" option of Visual Studio - any breakpoints in the open Solution will now be hit when code is ran by Interactive.

If you need the debugger without pre-existing code - you can now insert

System.Diagnostics.Debugger.Debug();

calls into the code you put into Interactive (but it does have to go in one 'chunk', i.e. one press of 'enter'), and you will be able to examine local vars via 'locals' window and run code against interactive context via 'immediate' window.

Translucid answered 11/1, 2022 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.