F# - 如何使用完全相同的命名参数调用C#重写方法

时间:2018-05-08 10:36:37

标签: f# c#-to-f#

如何使用完全相同的命名参数调用C#重写方法?

实施例

public static Task<CreateImageSummaryModel> CreateImagesFromDataAsync(this ITrainingApi operations, Guid projectId, IEnumerable<Stream> imageData, IList<Guid> tagIds = null, CancellationToken cancellationToken = default(CancellationToken));



public static Task<CreateImageSummaryModel> CreateImagesFromDataAsync(this ITrainingApi operations, Guid projectId, Stream imageData, IList<string> tagIds = null, CancellationToken cancellationToken = default(CancellationToken));

相同的方法名称和参数名称,但args具有不同的签名。

现在尝试调用第一个方法

let uploadStreams (tag: string) (streams: Stream seq) (projectId: Guid) (trainingApi: TrainingApi) = 

    let tag = trainingApi.CreateTag(projectId, tag)

    let tags = new List<_>([tag.Id])

    let streams = streams :> IEnumerable<Stream>

    trainingApi.CreateImagesFromDataAsync(projectId, imageData = streams, tagIds = tags)

这会产生编译错误

Severity    Code    Description Project File    Line    Suppression State
Error   FS0001  The type 'IEnumerable<Stream>' is not compatible with the type 'Stream' 

Severity    Code    Description Project File    Line    Suppression State
Error   FS0193  Type constraint mismatch. The type     'IEnumerable<Stream>'    is not compatible with type    'Stream' 

Severity    Code    Description Project File    Line    Suppression State
Error   FS0001  The type 'List<Guid>' is not compatible with the type 'IList<string>'   VisionAPI   

通常当我处理F#中的重写方法时,我只使用显式参数名称,例如

let x = cls.someOverriddenMethod(arg1 = 1)

但在这种情况下,这不起作用。

在这种情况下我该如何处理?

谢谢

2 个答案:

答案 0 :(得分:3)

我认为问题是imageData不是一个可选参数,但是您将它传递给它是一个可选参数。只需直接传递streams,而不是使用imageData = streams。这是一个为我编写的最小工作示例:

open System
open System.IO

type MyType () =
    static member A(a: string, b: Guid, c: Stream seq, ?d: Guid list) = ()
    static member A(a: string, b: Guid, c: Stream, ?d: Guid list) = ()

let f (streams: Stream seq) guids =
    MyType.A("", Guid.Empty, streams, d = guids)
    MyType.A("", Guid.Empty, streams |> Seq.head, d = guids)

答案 1 :(得分:0)

发生这些定义来自CustomVision API 1.0

public static Task<CreateImageSummaryModel> CreateImagesFromDataAsync(this ITrainingApi operations, Guid projectId, IEnumerable<Stream> imageData, IList<Guid> tagIds = null, CancellationToken cancellationToken = default(CancellationToken));



public static Task<CreateImageSummaryModel> CreateImagesFromDataAsync(this ITrainingApi operations, Guid projectId, Stream imageData, IList<string> tagIds = null, CancellationToken cancellationToken = default(CancellationToken));

我的F#app使用版本API 1.2,其中第一种方法不再存在(这很奇怪)

至少解开了神秘。