C# 从C调用F#库#

C# 从C调用F#库#,c#,f#,C#,F#,我有一个C#使用的F#类库 F#库有两种类型和一个简单函数,我想从我的C#代码中调用它们 命名空间HFNZ.InfectionStatus.ClassLibrary type Result = | Positive | Negative type TestType = | AntiHBc of Option<Result> | AntiHBs of Option<Result> | HBeAg of Option<Res

我有一个C#使用的F#类库

F#库有两种类型和一个简单函数,我想从我的C#代码中调用它们

命名空间HFNZ.InfectionStatus.ClassLibrary

type Result = 
    | Positive
    | Negative

type TestType = 
    | AntiHBc of Option<Result>
    | AntiHBs of Option<Result>
    | HBeAg of Option<Result>
    | HBsAg of Option<Result>
    | HBVDNA of Option<Result>

module Program =

    let getHVBStatus (test1, test2, test3, test4, test5) =
        match test1 test2 test3 test4 test5 with
        | AntiHBc   (Some(Positive)), 
          AntiHBs   (Some(Positive)), 
          HBeAg     (Some(Positive)), 
          HBsAg     (Some(Positive)), 
          HBVDNA    (Some(Positive))  -> "Normal"

        | _ -> "Elevated"
类型结果=
|肯定的
|否定的
类型TestType=
|期权抗HBC
|期权的抗HBs
|选择权HBeAg
|乙肝表面抗原选择
|期权的HBVDNA
模块程序=
让我们获取hvbstatus(test1、test2、test3、test4、test5)=
将test1 test2 test3 test4 test5与
|抗-HBc(部分(阳性)),
抗-HBs(部分(阳性)),
HBeAg(部分(阳性)),
HBsAg(部分(阳性)),
HBVDNA(部分(阳性))->“正常”
|——>“高架”
这是我的C#代码:

var正值=新的fsharp选项(Result.正值);
var-antiHBc=TestType.NewAntiHBc(阳性);
var抗-HBs=测试类型。新抗-HBs(阳性);
var HBeAg=TestType.NewHBeAg(阳性);
var HBsAg=试验类型。新HBsAg(阳性);
var HBVDNA=TestType.NewHBVDNA(阳性);
计划。getHVBStatus(抗-HBc、抗-HBs、HBeAg、HBsAg、HBVDNA);
由于以下错误,C代码无法编译:

Severity    Code    Description Project File    Line    Suppression State
Error   CS1503  Argument 1: cannot convert from 'InfectionStatus.ClassLibrary.TestType' to 'Microsoft.FSharp.Core.FSharpFunc<InfectionStatus.ClassLibrary.TestType, Microsoft.FSharp.Core.FSharpFunc<InfectionStatus.ClassLibrary.TestType, Microsoft.FSharp.Core.FSharpFunc<InfectionStatus.ClassLibrary.TestType, Microsoft.FSharp.Core.FSharpFunc<InfectionStatus.ClassLibrary.TestType, System.Tuple<InfectionStatus.ClassLibrary.TestType, InfectionStatus.ClassLibrary.TestType, InfectionStatus.ClassLibrary.TestType, InfectionStatus.ClassLibrary.TestType, InfectionStatus.ClassLibrary.TestType>>>>>'    UnitTestProject1    D:\F#\Code\InfectionStatus.ClassLibrary\UnitTestProject1\UnitTest1.cs   21  Active
严重性代码描述项目文件行抑制状态
错误CS1503参数1:无法从“InfectionStatus.ClassLibrary.TestType”转换为“Microsoft.FSharp.Core.FSharpFunc”UnitTestProject1 D:\F#\Code\InfectionStatus.ClassLibrary\UnitTestProject1\UnitTest1.cs 21 Active
我尝试将F#函数改为使用元组而不是curry,但仍然出现编译错误

将多个参数(非原语类型)从C#传递到F#的正确方法是什么

在F#function
getHVBStatus
中,要重新进行模式匹配的参数之间缺少逗号(在
match
行上)。因此,F#type推断将
test1
转换为一个函数,该函数将
test2
和所有其他参数作为参数

仅从C#编译器错误消息中很难发现这一点,但是如果您将鼠标悬停在
test1
上以查看F#中的推断类型,您将立即看到有问题。在
match
行中添加逗号可以解决以下问题:

let getHVBStatus (test1, test2, test3, test4, test5) =
    match test1, test2, test3, test4, test5 with
    | AntiHBc   (Some(Positive)), 
      AntiHBs   (Some(Positive)), 
      HBeAg     (Some(Positive)), 
      HBsAg     (Some(Positive)), 
      HBVDNA    (Some(Positive))  -> "Normal"
    | _ -> "Elevated"
在F#function
getHVBStatus
中,要重新进行模式匹配的参数之间缺少逗号(在
match
行上)。因此,F#type推断将
test1
转换为一个函数,该函数将
test2
和所有其他参数作为参数

仅从C#编译器错误消息中很难发现这一点,但是如果您将鼠标悬停在
test1
上以查看F#中的推断类型,您将立即看到有问题。在
match
行中添加逗号可以解决以下问题:

let getHVBStatus (test1, test2, test3, test4, test5) =
    match test1, test2, test3, test4, test5 with
    | AntiHBc   (Some(Positive)), 
      AntiHBs   (Some(Positive)), 
      HBeAg     (Some(Positive)), 
      HBsAg     (Some(Positive)), 
      HBVDNA    (Some(Positive))  -> "Normal"
    | _ -> "Elevated"