gpt4 book ai didi

c# - 如何从 F# 使用 C# 对象?

转载 作者:可可西里 更新时间:2023-11-01 02:59:22 25 4
gpt4 key购买 nike

我有以下 C# 代码。

namespace MyMath {
public class Arith {
public Arith() {}
public int Add(int x, int y) {
return x + y;
}
}
}

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

open MyMath.Arith
let x = Add(10,20)

当我运行以下命令时

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 环境中使用了单声道。

最佳答案

尝试

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

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

关于c# - 如何从 F# 使用 C# 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3901805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com