I'm not sure why you want to avoid using F# quotations - under the cover, they are pretty much the same thing as C# expression trees and if you want to create an expression tree in F#, the compiler will be using quotations under the cover in any case...
Anyway, you can do this without writing explicit <@ .. @>
because the compiler can automatically quote a function when it is passed as an argument to a method. So you can do:
type Expr =
static member Quote(e:Expression<System.Func<int, int>>) = e
let two = 2
let expr = Expr.Quote(fun x -> x * two)
expr.Compile().Invoke(4) // desired to return 8
EDIT: However, this really compiles to an F# quotation wrapped in a call that converts it to C# expression tree. So, in the end, you'll get the same thing as if you wrote:
open Microsoft.FSharp.Linq.RuntimeHelpers
let two = 2
let expr =
<@ System.Func<_, _>(fun x -> x * two) @>
|> LeafExpressionConverter.QuotationToExpression
|> unbox<Expression<Func<int, int>>>
expr.Compile().Invoke(4) // desired to return 8
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…