To complete Marcelo's answer yes, you can use quotes for this task:
open Microsoft.FSharp.Quotations open Microsoft.FSharp.Quotations.Patterns let velocity = 5 let fn (e:Expr) = match e with | PropertyGet (e, pi, li) -> pi.Name | _ -> failwith "not a let-bound value" let name = fn <@ velocity@ > printfn "%s" name
As you can see in the code, F # let-bound values โโof the upper definition (functions or variables) are implemented as properties of the class.
I can no longer find a link that shows how part of the F # code can be rewritten functionally using C #. Seeing the code makes it obvious why you need a PropertyGet .
Now, if you also want to evaluate the expression, you will need to install F # powerpack and the FSharp.PowerPack.Linq link in your project.
It adds the EvalUntyped method to the EvalUntyped class ..
open Microsoft.FSharp.Linq.QuotationEvaluation let velocity = 5 let fn (e:Expr) = match e with | PropertyGet (eo, pi, li) -> pi.Name, e.EvalUntyped | _ -> failwith "not a let-bound value" let name, value = fn <@ velocity@ > printfn "%s %A" name value
If you need to do this for an instance method, here is how I would do it:
let velocity = 5 type Foo () = member this.Bar (x:int) (y:single) = x * x + int y let extractCallExprBody expr = let rec aux (l, uexpr) = match uexpr with | Lambda (var, body) -> aux (var::l, body) | _ -> uexpr aux ([], expr) let rec fn (e:Expr) = match e with | PropertyGet (e, pi, li) -> pi.Name | Call (e, mi, li) -> mi.Name | x -> extractCallExprBody x |> fn | _ -> failwith "not a valid pattern" let name = fn <@ velocity@ > printfn "%s" name let foo = new Foo() let methodName = fn <@ foo.Bar@ > printfn "%s" methodName
To return to the code snippet showing the use of EvalUntyped , you can add an explicit type parameter for Expr and downcast ( :?> ) If you want / should store things in a type:
let fn (e:Expr<ยดT>) =