Call a higher order F# function from C#

前端 未结 3 1728
北海茫月
北海茫月 2021-02-01 19:56

Given the F# higher order function (taking a function in parameter):

let ApplyOn2 (f:int->int) = f(2)  

and the C# function

         


        
3条回答
  •  灰色年华
    2021-02-01 20:26

    To get an FSharpFunc from the equivalent C# function use:

    Func cs_func = (i) => ++i;
    var fsharp_func = Microsoft.FSharp.Core.FSharpFunc.FromConverter(
        new Converter(cs_func));
    

    To get a C# function from the equivalent FSharpFunc, use

    var cs_func = Microsoft.FSharp.Core.FSharpFunc.ToConverter(fsharp_func);
    int i = cs_func(2);
    

    So, this particular case, your code might look like:

    Func cs_func = (int i) => ++i;
    int result = ApplyOn22(Microsoft.FSharp.Core.FSharpFunc.FromConverter(
                new Converter(cs_func)));
    

提交回复
热议问题