8

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

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

4

1 回答 1

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!");
        }
    }
}

然后在 F# 交互中:

> #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 = ()

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

于 2011-02-25T07:55:41.803 回答