How to call the method from a MethodCallExpression in c#

后端 未结 5 1670
生来不讨喜
生来不讨喜 2020-12-29 06:33

I have a method call expression and try to invoke the method. I figured out a way, but I have problems in retrieving the parameter values since not every argument is describ

相关标签:
5条回答
  • 2020-12-29 06:49

    You don't need to worry about retrieving the arguments and calling the MethodInfo yourself, you can let .NET do it for you. All you need to do is create a Lambda expression containing that method.

    eg.

    MethodCallExpression expression = GetExpressionSomeHow();
    object result = Expression.Lambda(expression).Compile().DynamicInvoke();
    

    That's how I deal with nested queries in my Linq provider anyway.

    EDIT: Actually, it looks like you might already have a LambdaExpression in the selector variable. In that case, you should be able to just compile and invoke it directly:

    object result = selector.Compile().DynamicInvoke();
    
    0 讨论(0)
  • 2020-12-29 06:55

    Compiling an expression is a very intensive operation, so I would only do that if you are planning on re-using the expression. I would recommend the reflection way otherwise; you will find it executes faster. Never call expression.Compile() in a tight loop.

    0 讨论(0)
  • 2020-12-29 07:01

    If you want to compile your expression.call into a Action or Func, this is how you do:

    var method = typeof(MyType).GetMethod(nameof(MyType.MyMethod), BindingFlags.Public | BindingFlags.Static);
    var parameter = Expression.Parameter(typeof(string), "s");
    var call = Expression.Call(method, parameter);
    var lambda = Expression.Lambda<Func<string, int>>(call, call.Arguments.OfType<ParameterExpression>());
    var func = lambda.Compile();
    int result = func("sample string input");
    

    This allows you to simply do func.Invoke("mystring") or func("my string");

    The secret here is you need to pass the same parameters you used when creating the Expression.Call, otherwise you get an error of type "InvalidOperationException" variable 's' of type 'System.String' referenced from scope '', but it is not defined.

    0 讨论(0)
  • 2020-12-29 07:06

    I would try this to return the object:

    private static object _getValue(MethodCallExpression expression)
    {
        var objectMember = Expression.Convert(expression, typeof(object));
    
        var getterLambda = Expression.Lambda<Func<object>>(objectMember);
    
        var getter = getterLambda.Compile();
    
        return getter();
    }
    

    It is much faster can calling the following:

    LambdaExpression l = Expression.Lambda(Expression.Convert(element, element.Type));
    return l.Compile().DynamicInvoke();
    
    0 讨论(0)
  • 2020-12-29 07:08

    @Ch00k <-- Thanks, nice explanation. I would just like to add that

    selector.Compile();
    

    gives you a delegate. For an instance method you need an instance on which to call this method. You pass this instance as the argument to DynamicInvoke ala

    // Grab the method from MyClass - param1 and param2 are the actual parameters you
    // want to pass to the method call.
    Expression<Func<MyClass, TValue>> selector = (x => x.MyMethod(param1, param2));
    
    // Create an instance of MyClass to call the method on
    var myClass = new MyClass();
    
    // Call the method on myClass through DynamicInvoke
    object returnValue = selector.Compile().DynamicInvoke(myClass);
    
    0 讨论(0)
提交回复
热议问题