How to call the method from a MethodCallExpression in c#

后端 未结 5 1703
生来不讨喜
生来不讨喜 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 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>(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();
    

提交回复
热议问题