简体   繁体   English

通过Linq表达式树MethodCallExpression节点调用F#函数?

[英]Calling a F# function via a Linq expression tree MethodCallExpression node?

I am trying to create an expression tree containing a function call to a F# function on a certain module. 我正在尝试创建一个表达式树,其中包含某个模块上对F#函数的函数调用。 However, I am missing something because the System.Linq.Expressions.Expression.Call() helper function cant find the function I'm supplying. 但是,我缺少了一些东西,因为System.Linq.Expressions.Expression.Call()辅助函数无法找到我要提供的函数。

The Call() call gives an InvalidOperationException : " No method 'myFunction' on type 'TestReflection.Functions' is compatible with the supplied arguments. " “ Call()调用给出了InvalidOperationException :” 类型'TestReflection.Functions'上的方法'myFunction'与提供的参数不兼容。

If anyone can give me a hint on what I am doing wrong it would be very helpful. 如果有人可以给我提示我做错了什么,那将非常有帮助。

See the code below: 请参见下面的代码:

namespace TestReflection

open System.Linq.Expressions

module Functions =
    let myFunction (x: float) =
        x*x

    let assem = System.Reflection.Assembly.GetExecutingAssembly()
    let modul = assem.GetType("TestReflection.Functions")
    let mi = modul.GetMethod("myFunction")
    let pi = mi.GetParameters()

    let argTypes =
        Array.map 
            (fun (x: System.Reflection.ParameterInfo) -> x.ParameterType) pi

    let parArray = 
        [| (Expression.Parameter(typeof<float>, "a") :> Expression); |]
    let ce = Expression.Call(modul, mi.Name, argTypes, parArray)

    let del = (Expression.Lambda<System.Func<float, float>>(ce)).Compile()

printf "%A" (Functions.del.Invoke(3.5))

Regards, Rickard 问候,里卡德

The third argument to Expression.Call is an array of generic type parameters - your method is not generic, so that should be null . Expression.Call的第三个参数是通用类型参数的数组-您的方法不是通用的,因此应为null You'll also need to pass your "a" argument to Expression.Lambda : 您还需要将您的“ a”参数传递给Expression.Lambda

    let a = Expression.Parameter(typeof<float>, "a")
    let parArray = [| (a :> Expression); |]
    let ce = Expression.Call(modul, mi.Name, null, parArray)

    let del = (Expression.Lambda<System.Func<float, float>>(ce, a)).Compile()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM