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

发布于 2024-10-04 14:17:58 字数 344 浏览 9 评论 0原文

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

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

I have a set of static utility methods including unit tests. But I'd like to have a more interactive way to employ a testing -> fixing -> compiling cycle (REPL) like in Lisp or Smalltalk where one can immediately execute code in interactive mode. I tried to use F# Interactive to test these methods directly from within the opened C# project in VS 2010, but I didn't get it to work.

I know that I have to load the assembly (#r directive), open the namespace and then can call the methods (and inspect the result). But how do I do it within “F# Interactive” in Visual Studio 2010? I know it is possible with the “Immediate” window available in debug mode, but I want to do it within F# Interactive in "design mode", when I'm writing the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-10-11 14:17:58

您需要使用 #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 = ()

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

You need to include the path to your project using the #I directive then you may load your assembly and use it. I wrote a simple C# console app try this and got this to work.

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

Then 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();;
MESSAGE!
val it : unit = ()

So it definitely works, you just need to compile your projects first. Just remember to reset your session to release your assembly beforehand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文