简体   繁体   English

构建要在C#中使用的F#库

[英]Building F# library to be used in C#

Let's say I have the following function. 假设我有以下功能。

let rec fib n =
    match n with
    | 1 | 2 -> 1
    | n -> fib(n-1) + fib(n-2)

How can I compile this code into dll so that it's used from C#? 如何将此代码编译为dll,以便从C#中使用它?

I use both mono (in Mac OS X) and Visual Studio 2010. 我同时使用mono(在Mac OS X中)和Visual Studio 2010。

ADDED 添加

I added the following to make namespace. 我添加了以下内容来创建命名空间。

namespace MyMath.Core
module public Process=

And fsc --target:library facto.fs gives me facto.dll. 和fsc --target:library facto.fs给了我facto.dll。

The C# code to use this code as follows. 使用此代码的C#代码如下。

using System;
using MyMath.Core;

class TestFSharp {
    public static void Main() {
        int val = MyMath.Core.Process.fib(10);
        Console.WriteLine(val);
    }
}

dmcs /r:facto.dll testf.cs gives me tesetf.exe. dmcs /r:facto.dll testf.cs给了我tesetf.exe。

Well, if you put this in the file Program.fs, and compile it into a library (with --target:library ), then it would appear from C# as 好吧,如果你将它放在Program.fs文件中,并将其编译成一个 (带有--target:library ),那么它将从C#出现

Program.fib(5)

See also call F# dll in C# 另请参阅C#中的调用F#dll

However if you plan to expose F# code to C#, you may want to uses namespaces and types. 但是,如果您计划将F#代码公开给C#,则可能需要使用名称空间和类型。 See the F# Component Design Guidelines for more. 有关更多信息,请参阅F#组件设计指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM