Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in Visual Studio 2012 to use F# REPL when debugging c#

If I have a breakpoint in a c# program I would love to use the F# REPL to inspect my code. Is this possible in any way?

like image 938
bradgonesurfing Avatar asked Apr 29 '13 05:04

bradgonesurfing


People also ask

How do I go back to F12 in Visual Studio?

In Visual Studio Code, you can also use Alt + Left-Arrow to return to your original location in the source code after navigating with F12.

What does Ctrl F do in Visual Studio?

You've probably already discovered the search shortcut Ctrl-F, but did you know Visual Studio Code has different types of find features built-in? Ctrl + F as mentioned above this shortcut will find whatever you search for in the current file.

What is Alt Shift F VS Code?

Search for Format Document to find out which keybinding is currently able to format your code. You can choose to continue with the default keybinding or change it by clicking on the edit icon on the left of the command you're interested in changing. In this case, you would simply hit Alt + Shift + F and that's it.

How do you Shift text in VS Code?

move the cursor at the upper left corner of the block of text you want to shift; click with the mouse holding down shift + alt, on the down left corner of the block of text you want to shift; now you could insert as many spaces as you want.


2 Answers

You should be able to debug a C# project with the F# REPL -- I do it when debugging F# library projects, but C# applications/libraries will work too (I think).

Compile your C# project. In F# interactive, reference the C# assembly using the #r directive (see ArthurFSharp's post). Insert a breakpoint somewhere in your C# code.

In Visual Studio, go to Tools -> Attach to Process. Find Fsi.exe in the list of processes and double-click it; if you don't see it there, make sure the Fsi process has been started (just clicking inside the F# Interactive window should do it).

In F# Interactive, execute some bit of code that'll reach the breakpoint you set; the debugger should stop execution on the breakpoint just like you want. As long as the debugger isn't stopped on some breakpoint, you can also execute other code from within FSI, for example to change settings in the running C# application.

IMPORTANT : Remember that Windows locks an assembly whenever it is loaded into a process. This means that you won't be able to recompile your C# app as long as FSI is open! The fix is simple though -- once you've made some changes to your C# app and want to recompile and begin testing again, just right-click within the F# Interactive window and click 'Reset Interactive Session' in the context-menu. The process will be killed and restarted, so you can begin all over again. (If you do forget to restart the FSI process and try to recompile your application, compilation will take a bit longer than normal, then you'll get an error message telling you the compiler couldn't write to the output file.)

like image 64
Jack P. Avatar answered Oct 12 '22 18:10

Jack P.


You need to include the path to your project using the #I directive then you may load your assembly and use it. Then load the assembly (#r directive), open the namespace and then can call the methods (and inspect the result).

for example :

using System;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            PrintMessage();
        }

        public static void PrintMessage()
        {
            Console.WriteLine("Hello World!");
        }
    }
}

And in F# Interactive :

> #I "full path to debug directory";;

--> Added 'full path to debug directory' to library include path

> #r "ConsoleApplication1.exe";;

--> Referenced 'full path to debug directory\ConsoleApplication1.exe'

> open ConsoleApplication1;;
> Program.PrintMessage();;
Hello World!
val it : unit = ()

ps : need to compile your projects first.

like image 20
ArthurCPPCLI Avatar answered Oct 12 '22 20:10

ArthurCPPCLI