简体   繁体   English

将FSharpFun从C#传递到F#

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

F# Definition of a function in a module. F#模块中函数的定义。

module ClassLibrary1.Functions

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

f# unit tests - works F#单元测试-作品

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

in c# unit test 在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);
        }
     }
}

Error 错误

Cannot conver initializer type "lambda expression" to target type 无法将初始化程序类型“ lambda表达式”转换为目标类型


Edit 编辑

Screenshots to help the answer. 截图有助于解答。

在此处输入图片说明

Screenshot 3 屏幕截图3

在此处输入图片说明

For the lastest F# version (10.2.3), use 对于最新的F#版本(10.2.3),请使用

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

With FX_NO_CONVERTER defined: 定义了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