开发者

Building F# library to be used in C#

开发者 https://www.devze.com 2023-01-19 14:36 出处:网络
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)

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#?

I use both mono (in Mac OS X) and 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.

The C# code to use this code as follows.

using开发者_Python百科 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.


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.fib(5)

See also call F# dll in C#

However if you plan to expose F# code to C#, you may want to uses namespaces and types. See the F# Component Design Guidelines for more.

0

精彩评论

暂无评论...
验证码 换一张
取 消