在 C# 中,可以分配公共(public)成员来代替构造函数调用:
options.TokenValidationParameters = new TokenValidationParameters { NameClaimType = "preferred_username" }
为了达到同样的效果,我写了下面的F#代码:
options.TokenValidationParameters <- (new TokenValidationParameters()) |> fun it -> (it.NameClaimType <- "preferred_username"; it)
然而,它看起来一点也不好看。
有没有更好的方法来做到这一点?理想的解决方案可能是像 kotlin 中的 apply
这样的函数?
感谢任何帮助。
最佳答案
如评论中所述,您可以使用对象初始化语法指定参数:
options.TokenValidationParameters <-
new TokenValidationParameters( NameClaimType = "preferred_username")
以上绝对是最好的方法。但是,即使您需要更复杂的逻辑,也没有必要使用笨拙的管道和 fun
构造在一行中构造对象。只定义一个局部变量没有错:
let tokParams = new TokenValidationParameters()
tokParams.NameClaimType <- "preferred_username"
options.TokenValidationParameters <- tokParams
如果你想限制 tokParams
变量的范围,你可以这样做:
options.TokenValidationParameters <-
let tokParams = new TokenValidationParameters()
tokParams.NameClaimType <- "preferred_username"
tokParams
这也有效,因为 F# block 被视为表达式。在这种特殊情况下,您不需要这样做,但了解它可能会有所帮助 - 并且它可以让您避免笨拙的管道。
https://stackoverflow.com/questions/71673752/
相关文章:
中的特">c# - 我怎样才能得到所有字符串 "greater"的列表而不是 List
aws-lambda - SQS 到 ECS (Fargate) 或 SQS 到 Lambda 到
google-forms - 您可以向 google forms api 提交 Restful 请求
javascript - react : Difference between side effec
python - 等于运算符与元组 : 'a' , 'b' == ('a' , 'b' )
python - 连接到与登录到 google colab 的不同的 google 驱动器