如何使用F#Interactive以交互方式测试C#函数

Nic*_*ico 8 c# f# visual-studio-2010 f#-interactive read-eval-print-loop

我有一组静态实用程序方法,包括单元测试.但是我希望有一种更加互动的方式来使用测试 - >修复 - >编译循环(REPL),就像在Lisp或Smalltalk中一样,可以立即在交互模式下执行代码.我尝试使用F#Interactive直接在VS 2010中打开的C#项目中测试这些方法,但我没有让它工作.

我知道我必须加载程序集(#r指令),打开命名空间然后可以调用方法(并检查结果).但是我如何在Visual Studio 2010中的"F#Interactive"中进行此操作?我知道在调试模式下可以使用"立即"窗口,但是当我编写代码时,我希望在"设计模式"下​​的F#Interactive中进行.

Jef*_*ado 10

您需要使用#I指令包含项目的路径,然后您可以加载程序集并使用它.我写了一个简单的C#控制台应用试试这个并让它工作.

using System;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            PrintMessage();
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
            Console.WriteLine();
        }

        public static void PrintMessage()
        {
            Console.WriteLine("MESSAGE!");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在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();;
MESSAGE!
val it : unit = ()
Run Code Online (Sandbox Code Playgroud)

所以它肯定有效,你只需要先编译你的项目.请记住重置会话以事先释放您的程序集.