将 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);
}
}
}
错误
Cannot conver initializer type "lambda expression" to target type
编辑
帮助回答的截图。
截图 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));
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);
}
}
}
错误
Cannot conver initializer type "lambda expression" to target type
编辑
帮助回答的截图。
截图 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));