How do I programmatically build up the following Quotation Expression in F#? -
i have need access .net dictionary within quotation expression. following simplified version:
let mutable dict:dictionary<string, float> = dictionary() dict.add("first", 1.0) dict.add("second", 2.0) let qe2 = <@ dict.["second"] @> the output of qe2 follows:
val qe2 : quotations.expr<float> = propertyget (some (propertyget (none, dict, [])), item, [value ("second")]) all fine when can directly create quotation expression directly quoted code above. how achieve same thing building qe2 programmatically?
i know need somehow use quotations.expr.propertyget twice in nested fashion don't know how obtain neccesary propertyinfo , methodinfo objects go doing that.
the key know let dict declaration compiled property on static module class.
here working sample.
module quotationstest open microsoft.fsharp.quotations open system open system.collections.generic open system.reflection let mutable dict:dictionary<string, float> = dictionary() dict.add("first", 1.0) dict.add("second", 2.0) let qe2 = <@ dict.["second"] @> let moduletype = type.gettype("quotationstest") let dictprop = moduletype.getproperty("dict", bindingflags.static ||| bindingflags.public) let indxprop = dict.gettype().getproperty("item", [| typeof<string> |]) let qe1 = expr.propertyget(expr.propertyget(dictprop, []), indxprop, [ expr.value("first") ]) printfn "%a" qe1 printfn "%a" qe2 compiling , running above results in following:
> fsc quotationstest.fs microsoft (r) f# compiler version 12.0.30815.0 copyright (c) microsoft corporation. rights reserved. > quotationstest.exe propertyget (some (propertyget (none, dict, [])), item, [value ("first")]) propertyget (some (propertyget (none, dict, [])), item, [value ("second")])
Comments
Post a Comment