Well, you need to expand the expression, find MethodCallExpression , and then look at the arguments. Note that we do not have the value o , so we must assume that the arguments to the method do not rely on this. Also, we still assume that the lambda expression simply relies on the fact that it is a MethodCallExpression ?
EDIT: Well, here is an edited version that evaluates the arguments. However, it is assumed that you are not using the lambda expression parameter in the arguments (this is what new object[1] says - it provides an effective null parameter).
using System; using System.Linq.Expressions; class Foo { public void Save(int x, string y, int z, double d) { } } class Program { static void Main() { var x = 1; var a = 2; var b = 3; ShowValues<Foo>(o => o.Save(x, "Jimmy", a + b + 5, Math.Sqrt(81))); } static void ShowValues<T>(Expression<Action<T>> expression) { var call = expression.Body as MethodCallExpression; if (call == null) { throw new ArgumentException("Not a method call"); } foreach (Expression argument in call.Arguments) { LambdaExpression lambda = Expression.Lambda(argument, expression.Parameters); Delegate d = lambda.Compile(); object value = d.DynamicInvoke(new object[1]); Console.WriteLine("Got value: {0}", value); } } }
Jon skeet
source share