Sunday, January 22, 2012

MethodInfo <--> Quotation

When I think about the meta programming with quotation, I realize both the reflection and quotation are all meta programming which can provide the program information. Reflection is nothing new and I learn quotation for a while. Now the problem becomes how to make these two systems work together.

thnaks to my colleagues: Keith and Vlad, the solution to this problem is:

From methodInfo to quotation:

    type T =
        static member M(x) = 1 + x
    let methodInfo = typeof< T >.GetMethod("M")
    let e = Quotations.Expr.TryGetReflectedDefinition methodInfo

From quotation to methodInfo:

       match <@ T.M 1 @> with
    | Quotations.Patterns.Call(None, mi, args) -> mi
    | _ -> failwith "Expected Call"

No comments: