将 C# 中的动态类型转换为 F#

Casting a dynamic type in C# to F#

我正在研究 Auth0 的文档 here 并将 C# 代码移植到 F#

在C#代码中,有这一行:

var auth0LoginResult = await _auth0Client.LoginAsync(new { audience = AuthenticationConfig.Audience });

其中 LoginAsync 的参数称为对象类型的 extraParameters。

Audience 只是一个字符串,但是当我尝试传入这样一个字符串时

let extraParameters = Support.authenticationConfig.Audience
let result = client.LoginAsync(extraParameters).Result

它失败了

System.Reflection.TargetParameterCountException: Number of parameters specified does not match the expected number.

如何传入方法喜欢的类型?只是将它投射到对象?

我猜该库采用 object 并使用反射来查看对象的成员,这就是为什么 C# 调用它的方式是给它一个匿名对象作为参数。

在 F# 中,您可能需要使用需要作为字段传递的参数来定义显式记录类型,尽管在不久的将来,您可能能够使用 anonymous records for this.

我还没有测试过这个,但我会尝试这样的事情:

type LoginParameters = { audience : string }

async { 
  let! result = 
    auth0Client.LoginAsync({ audience = AuthenticationConfig.Audience }) 
    |> Async.AwaitTask
  (...) }