Pages

Sunday, August 21, 2011

F# quotation and expression tree

If have a function like "sample", how to write out the expression with F# quotation? I have been struggle this for whole night.


[ < ReflectedDefinition > ]
let sample (x) =
    printfn "###%A" x

It seems that we can only get the function parameters, and the methodInfo stops the expression traversing. Here is a way to solve this problem:

match expression with

| Patterns.Call (expOption, mi, expList)  ->
        matchExp expOption
        match mi with
            | DerivedPatterns.MethodWithReflectedDefinition n -> recursive call to access n
            | _ -> print mi

No comments: