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

标签 c# f#

我有以下 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/

相关文章:

c# - 构造函数是在C#中初始化类中非空属性的唯一方法吗?

winforms - 构建一个可取消的非阻塞后台 worker

function - F# 中 let、fun 和 function 之间的区别

.net - 点积函数

c# - 使用 ExecuteTransactionRequest 创建相关实体

c# - 在 .NET Framework 类库项目中安装 Entity Framework Core 2.0 会产生无效引用

c# - 显示属性中的 ASP .Net Core 3.0 ShortName (DataAnnotations)

generics - F# 将泛型参数约束为枚举类型

F# 读取固定宽度的文本文件

c# - 这些 LINQ 查询之间有什么区别