如何使用 F# 中的 C# 对象? [英] How to use C# object from F#?

查看:19
本文介绍了如何使用 F# 中的 C# 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 C# 代码.

命名空间 MyMath {公共课算术{公共算术(){}公共 int 添加(int x,int y){返回 x + y;}}}

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

打开 MyMath.Arith让 x = 添加(10,20)

当我运行以下命令时

<前>fsc -r:MyMath.dll testcs.fs

我收到此错误消息.

<前>/Users/smcho/Desktop/cs/namespace/testcs.fs(1,13): 错误 FS0039: 命名空间Arith"是没有定义的/Users/smcho/Desktop/cs/namespace/testcs.fs(3,9): 错误 FS0039: 值或构造函数添加"未定义

可能有什么问题?我在 .NET 环境中使用了单声道.

解决方案

try

打开MyMathlet arith = Arith()//创建 Arith 的实例let x = arith.Add(10, 20)//调用方法 Add

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

I have the following C# code.

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

And I came up with the F# code named testcs.fs to use this object.

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

When I run the following command

fsc -r:MyMath.dll testcs.fs

I got this error message.

/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

What might be wrong? I used mono for .NET environment.

解决方案

try

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

Arith in your code is class name, you cannot open it like namespace. Possibly you are confused with ability to open F# modules so its functions can be used without qualification

这篇关于如何使用 F# 中的 C# 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆