最近,我从当前项目中中断了 2 周,以编写一个大小合适的文件解析器和一个数字错误检查器。我决定用 F# 来编写它们,以供欢笑。了不起的决定。
用 VB 编写的旧版本程序超过 1000 行;我在 F# 的 170 中管理它。惊人的。
我现在回到了我当前的项目,并希望合并一个 F# 库来进行一些解析,并可能编写/读取 XML 保存文件。但我似乎无法弄清楚如何从 C# WPF 应用程序中调用 F# 库。
我正在使用 Microsoft Visual 2010 Professional,这是我迄今为止参考这篇文章所做的尝试:http: //www.devjoy.com/2013/02/c-to-f-interop-sharing-a-domain-model / 和几个 SO 帖子:
- 创建一个 WPF 项目。
- 向解决方案添加了 F# 库项目。
- 通过以下方式向 C# 项目添加了对 F# 库的引用:在 SolutionExplorer 中右键单击项目文件,单击“添加引用”,从项目选项卡中选择“MyFSharpLib”,然后单击添加。
然后我在默认的 F# 文件 module1.fs 中添加了一些测试代码:
module Module1 let Add a b = a + b
并尝试从 C# 调用它:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication2 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } // Call F# library private void Button_Click(object sender, RoutedEventArgs e) { int a = Module1.Add(1, 2); } } }
这会导致错误提示:“名称 'Module1' 在当前上下文中不存在。”
我认为问题出在程序集上。我注意到在我见过的几个代码示例中有using Microsoft.FSharp.Core
. 这就是我悲伤的根源吗?我尝试添加它,但在 .NET 选项卡下找不到它。
这让我发疯,任何帮助将不胜感激。