gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:02:09 26 4
gpt4 key购买 nike

如何使用完全相同的命名参数调用 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));

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

现在尝试调用第一个方法

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)

但在这种情况下这是行不通的。

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

谢谢

最佳答案

我认为问题在于 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)

关于F# - 如何使用完全相同的命名参数调用 C# 覆盖的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50231565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com