简体   繁体   English

从 C# 调用 F# 库

[英]Invoking an F# library from C#

I have an F# class library that is consumed by C#.我有一个 C# 使用的 F# class 库。

The F# library has 2 two types and one simple function that I want to invoke from my C# code. F# 库有两种类型和一种简单的 function,我想从我的 C# 代码中调用它。

namespace HFNZ.InfectionStatus.ClassLibrary命名空间 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"

This is my C# code:这是我的 C# 代码:

var positive = new FSharpOption<Result>(Result.Positive);
var antiHBc =  TestType.NewAntiHBc(positive);
var AntiHBs = TestType.NewAntiHBs(positive);
var HBeAg = TestType.NewHBeAg(positive);
var HBsAg = TestType.NewHBsAg(positive);
var HBVDNA = TestType.NewHBVDNA(positive);

Program.getHVBStatus(antiHBc, AntiHBs, HBeAg, HBsAg, HBVDNA);

The C# code does not compile due to this error:由于此错误,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

I tried changing the F# function to use tuples instead of currying but still getting a compilation error.我尝试将 F# function 更改为使用元组而不是currying,但仍然出现编译错误。

What is the correct way to pass multiple arguments (non-primitive types) from C# to F#?将多个 arguments(非原始类型)从 C# 传递到 F# 的正确方法是什么?

In your F# function getHVBStatus , you are missing commas between the parameters that you want to pattern match agains (on the match line).在您的 F# function getHVBStatus中,您想要再次进行模式匹配的参数之间缺少逗号(在match行上)。 As a result, the F# type inference turns test1 into a function that takes test2 and all the other parameters as arguments.结果,F# 类型推理将test1转换为 function,它将test2和所有其他参数作为 arguments。

This is quite hard to spot just from the C# compiler error message, but if you hover over test1 to see the inferred type in F#, you'll immediately see that there's something wrong.仅从 C# 编译器错误消息中很难发现这一点,但如果您在test1上查看 hover 以查看 F# 中的推断类型,您会立即看到有问题。 Adding commas on the match line should fix the problem: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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM