繁体   English   中英

将FSharpFun从C#传递到F#

[英]Passing a FSharpFun from C# to F#

F#模块中函数的定义。

module ClassLibrary1.Functions

let checkThis item f =
    if f item then
        printfn "HIT"
    else
        printfn "MISS"

F#单元测试-作品

[<TestMethod>]
    member this.TestFunctions() =
        checkThis 5 (fun x -> x > 3)

在C#单元测试中

using ClassLibrary1;

namespace TestProject
{
    [TestClass]
    public class UnitTest1
    {

        [TestMethod]
        public void TestFunctions()
        {
            FSharpFunc<int, bool> s = x => x > 3 // error, how to declare?
            Functions.checkThis(5, s);
        }
     }
}

错误

无法将初始化程序类型“ lambda表达式”转换为目标类型


编辑

截图有助于解答。

在此处输入图片说明

屏幕截图3

在此处输入图片说明

对于最新的F#版本(10.2.3),请使用

var s = FuncConvert.FromFunc(new Func<int, bool>(x => x > 3));

定义了FX_NO_CONVERTER

FSharpFunc<int, bool> s = new Converter<int, bool>(x => x > 3); 
var s = FSharpFunc<int, bool>.FromConverter(x => x > 3);
var s = FuncConvert.ToFSharpFunc(new Converter<int, bool>(x => x > 3));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM