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

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

问题描述

我有以下的C#代码。

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

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

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

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

当我运行下面的命令


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

什么可能是错误的?我用单为.NET环境。

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

推荐答案

尝试

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

ARITH 的在你的代码的类名,你不能打开它就像命名空间。也许你感到困惑与开F#模块的能力,这样它的功能可以在没有资格使用

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

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

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