這裏有一個大致相當於:
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.Quotations.Patterns
type DynamicMember<'t,'u> = Expr<'t -> 'u>
let getValueReader (expr:DynamicMember<'recdT, 'fieldT>) =
// Match the quotation representing the symbol
match expr with
| Lambda(v, PropertyGet (Some (Var v'), pi, [])) when v = v' ->
// It represents reading of the F# record field..
// .. get a function that reads the record field using F# reflection
let rdr = Reflection.FSharpValue.PreComputeRecordFieldReader pi
// we're not adding any additional processing, so we just
// simply add type conversion to the correct types & return it
((box >> rdr >> unbox) : 'recdT -> 'fieldT)
| _ ->
// Quotation doesn't represent symbol - this is an error
failwith "Invalid expression - not reading record field!"
type SampleRec = { Str : string; Num : int }
let readStrField = getValueReader <@ fun (r : SampleRec) -> r.Str @>
let readNumField = getValueReader <@ fun (r : SampleRec) -> r.Num @>
let rc = { Str = "Hello world!"; Num = 42 }
let s, n = readStrField rc, readNumField rc
printfn "Extracted: %s, %d" s n
來源
2017-02-17 17:24:36
kvb
您正在尋找這樣的事情? http://stackoverflow.com/questions/17640218/accessing-dynamic-property-in-f – mydogisbox
@mydogisbox,是的,但我想訪問被靜態檢查。所以,如果屬性不存在,我會得到編譯錯誤。據我瞭解,托馬斯的解決方案就是這樣做的。 – alehro