簡體   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