我Record
通过 F# Data SqlClient 类型提供程序使用如下代码返回了一些数据:
open FSharp.Data
[<Literal>]
let connectionString = "..."
[<Literal>]
let linksQuery = "SELECT Id, TheName, ... FROM Links"
type LinksQuery = SqlCommandProvider<linksQuery, connectionString>
let result = (new LinksQuery())
.AsyncExecute()
|> Async.RunSynchronously
let first = result.[0]
我现在想first
Record
动态访问它的属性——我需要哪些属性取决于一些用户输入——他们输入key
.
我知道我可以使用 C# 风格的反射来做到这一点,代码如下:
let theType = first.GetType()
let theProp = theType.GetProperty("Item")
let theValue = theProp.GetValue(first, [| key |])
// now use `theValue` (which is sometimes a string and sometimes an Option(string)
但是,我想知道是否有任何方法可以使用更简单的 F#
理想情况下,我想使用类似的东西first.[key]
- 但此语句返回错误The field, constructor or member "Item" is not defined