最近我花了2周的沉寂,从我目前的项目写一个体面的大小文件分析器和一个数字错误检查。我决定把它们写在F#踢和笑声。意想不到的决定。
Recently I took a 2 week hiatus from my current project to write a decent sized file parser and a numerical error checker. I decided to write them in F# for kicks and giggles. Fantastic decision.
VB编写的程序的旧版本是1000线;我在F#中管理其在170。真棒。
The older version of the program written in VB was over 1000 lines; I managed it in 170 in F#. Awesome.
我回到了我当前的项目现在想将一个F#lib中做了一些分析,也许写入/读取XML保存文件。但我似乎无法弄清楚如何从一个C#WPF应用程序为我的生活调用F#库。
I'm back on my current project now and want to incorporate an F# lib to do some parsing and maybe writing/reading an XML save file. But I cannot seem to figure out how to call an F# library from a C# WPF application for the life of me.
我使用Microsoft Visual 2010专业版,这里就是我试图迄今引用这篇文章:的 http://www.devjoy.com/2013/02/c-to-f-interop-sharing-a-domain-model/ 和一对夫妇SO帖子:
I'm using Microsoft Visual 2010 Professional, here's what I've attempted thus far referencing this article: http://www.devjoy.com/2013/02/c-to-f-interop-sharing-a-domain-model/ and a couple SO posts:
创建一个WPF项目。 在增加了一个F#库项目的解决方案。 添加引用到F#库的C#通过项目:在SolutionExplorer右键点击项目文件,单击添加引用,选择MyFSharpLib从项目选项卡,然后单击添加然后添加一些测试code到默认的F#文件module1.fs: Create a WPF project. Added an F# library project to the solution. Added a reference to the F# library to C# project via: Right click project file in SolutionExplorer, click 'Add Reference', selected 'MyFSharpLib' from projects tab and click Add.
I then added some test code to the default F# file module1.fs:
module Module1
let Add a b = a + b
和试图从C#调用它:
And tried to call it from 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);
}
}
}
这将导致一个错误说:
我认为这个问题是与组件。我注意到,在我见过的有使用Microsoft.FSharp.Core
一对夫妇code样本。这是我痛苦的根源?我试着将它添加,但没有找到它在.NET选项卡下。
I think the problem is with the assemblies. I noticed in a couple code samples I've seen have using Microsoft.FSharp.Core
. Is this the source of my grief? I tried adding it and couldn't find it under the .NET tab.
这是推动我疯了,任何帮助将是非常美联社preciated。
This is driving me crazy, any help would be much appreciated.
推荐答案
看来你找到了答案已经 - 构建F#项目 - 但我会提供一个答案(为后人),以一个问题,这将几乎完全一样的症状,但不同的根本原因。
It seems you found the answer already -- building the F# project -- but I'm going to provide an answer (for posterity) to a problem which would have almost the exact same symptoms, but a different underlying cause.
如果您所提供的完全使用例如F#code,即用一个简单的名称的模块(没有命名空间),重要的是要知道,你需要使用全球
访问从C#的F#模块时的关键字。例如,这是怎么了,你不得不给你打电话从C#中定义的添加
功能:
If you use your example F# code exactly as provided, that is, a module with a simple name (without a namespace), it's important to know that you need to use the global
keyword when accessing the F# module from C#. For example, this is how you'd have to call the Add
function you defined from C#:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var result = global::MyModule.Add(1, 2);
}
}
}
除非你有特别的理由不,它通常只是一个更好的主意,用一个命名空间的模块,例如,模块MyNamespace.Module1
。
相关推荐
最新文章