问题
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?
回答1:
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.)
回答2:
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.
来源:https://stackoverflow.com/questions/16271945/is-there-a-way-in-visual-studio-2012-to-use-f-repl-when-debugging-c-sharp