13

How can I get the arguments values of a MethodCallExpression?

Today I do this way, but isn“t fast enough:

private static object GetArgumentValue(Expression element)
{
    LambdaExpression l = Expression.Lambda(Expression.Convert(element, element.Type));
    return l.Compile().DynamicInvoke();
}

This method get values from a Expression, but if I know that Expression always come from a MethodCallExpression.Arguments I can optimize it?

I think I can change first line to this, but I don't know if it works for all situations:

LambdaExpression l = Expression.Lambda(element);
2
  • You mean something like "object[] args = methodCallExpression.Arguments.Select(p => p.Value);"? – Please treat your mods well. Mar 25 '11 at 14:04
  • @Rodrigo That's what I'm looking for, but Value isn't a property of Expression. – Langdon Apr 12 '11 at 20:46
4

Cake

class Program
    {
        static void Main(string[] args)
        {
            Expression<Action<string>> action = a => Console.WriteLine("asdf");
            var mce = action.Body as MethodCallExpression;
            Console.WriteLine((mce.Arguments[0] as ConstantExpression).Value);
            Console.ReadKey();
        }
    }
3
  • Cake? (and then some filler text so the 5 letters comment can be posted) – joce May 20 '11 at 4:51
  • 8
    Why are you assuming that arguments will always be constants? – Felipe Fujiy Pessoto May 20 '11 at 11:40
  • 2
    because if it's not, you can't get anything out of it without .Compile() – Sleeper Smith May 20 '11 at 16:33
3

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 then calling the following:

LambdaExpression l = Expression.Lambda(Expression.Convert(element, element.Type));
return l.Compile().DynamicInvoke();
3
  • I am getting System.InvalidOperationException: 'No coercion operator is defined between types 'System.Void' and 'System.Object'.'. Running on .Net Core 2. – ttugates May 23 '18 at 13:08
  • @ttugates, from the error it looks like the lambda method you are calling returns a void, but this line Expression.Lambda<Func<object>>(objectMember) expects the function to return an object – TheMiddleMan Jun 15 '18 at 13:36
  • because its a Func and not an Action. Cool. Don't recall what I was testing against at the time. – ttugates Jun 15 '18 at 13:39
2

This worked for me:

private static object GetArgumentValue(Expression element)
{
    if (element is ConstantExpression)
    {
        return (element as ConstantExpression).Value;
    }

    var l = Expression.Lambda(Expression.Convert(element, element.Type));
    return l.Compile().DynamicInvoke();
}

It is combining the OP's original solution with Sleeper Smith's Answer.

Your Answer

By clicking ā€œPost Your Answerā€, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.