如何使用F#中的C#对象?

pro*_*eek 17 c# f#

我有以下C#代码.

namespace MyMath {
    public class Arith {
        public Arith() {}
        public int Add(int x, int y) {
            return x + y;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想出了名为testcs.fs的F#代码来使用这个对象.

open MyMath.Arith
let x = Add(10,20)
Run Code Online (Sandbox Code Playgroud)

当我运行以下命令

fsc -r:MyMath.dll testcs.fs

我收到此错误消息.

/Users/smcho/Desktop/cs/namespace/testcs.fs(1,13): error FS0039: The namespace 'Arith' is 
not defined

/Users/smcho/Desktop/cs/namespace/testcs.fs(3,9): error FS0039: The value or constructor 
'Add' is not defined

可能有什么问题?我在.NET环境中使用mono.

des*_*sco 16

尝试

open MyMath
let arith = Arith() // create instance of Arith
let x = arith.Add(10, 20) // call method Add
Run Code Online (Sandbox Code Playgroud)

您的代码中的Arith是类名,您无法像命名空间一样打开它.可能您对打开F#模块的能力感到困惑,因此可以无限制地使用其功能


kvb*_*kvb 7

既然Arith是类而不是命名空间,则无法打开它.你可以这样做:

open MyMath
let x = Arith().Add(10,20)
Run Code Online (Sandbox Code Playgroud)