简体   繁体   English

在Visual Studio 2012中有一种在调试C#时使用F#REPL的方法

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

If I have a breakpoint in ac# program I would love to use the F# REPL to inspect my code. 如果我在ac#程序中有断点,则希望使用F#REPL检查我的代码。 Is this possible in any way? 这有可能吗?

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). 您应该能够使用F#REPL调试C#项目-我在调试F#库项目时就执行了,但是C#应用程序/库也可以工作(我认为)。

Compile your C# project. 编译您的C#项目。 In F# interactive, reference the C# assembly using the #r directive (see ArthurFSharp's post). 在F#互动,参考使用该组件的C# #r指令(见ArthurFSharp的职位)。 Insert a breakpoint somewhere in your C# code. 在C#代码中的某处插入一个断点。

In Visual Studio, go to Tools -> Attach to Process . 在Visual Studio中,转到Tools -> Attach to Process Find Fsi.exe in the list of processes and double-click it; 在进程列表中找到Fsi.exe并双击它; 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). 如果您在此处看不到它,请确保Fsi进程已启动(只需在F#Interactive窗口中单击即可)。

In F# Interactive, execute some bit of code that'll reach the breakpoint you set; 在F#Interactive中,执行一些代码,这些代码将达到您设置的断点。 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. 只要调试器没有在某个断点处停止,您还可以在FSI中执行其他代码,例如更改正在运行的C#应用​​程序中的设置。

IMPORTANT : Remember that Windows locks an assembly whenever it is loaded into a process. 重要说明:请记住,只要将程序集加载到进程中,Windows就会将其锁定。 This means that you won't be able to recompile your C# app as long as FSI is open! 这意味着只要FSI打开,您将无法重新编译C#应用程序! 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. 不过,修复很简单-对C#应用程序进行了一些更改并想要重新编译并再次开始测试后,只需在F#Interactive窗口中右键单击并在上下文菜单中单击“重置交互式会话”即可。 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.) (如果您确实忘记了重新启动FSI进程并尝试重新编译应用程序,则编译将比正常情况花费更长的时间,那么您将收到一条错误消息,告知您编译器无法写入输出文件。)

You need to include the path to your project using the #I directive then you may load your assembly and use it. 您需要使用#I指令包括项目的路径,然后才能加载程序集并使用它。 Then load the assembly (#r directive), open the namespace and then can call the methods (and inspect the result). 然后加载程序集(#r指令),打开名称空间,然后可以调用方法(并检查结果)。

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 : 在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. ps:首先需要编译您的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM