It can't be done using an Action
, but a lambda expression can also be treated as an Expression<Action>
and then it becomes possible.
Note that the code below only works for this kind of expression: it uses the knowledge that we have a method call and that we use constants as parameters.
public class Program
{
public static void Main(string[] args)
{
var values = GetParams(() => Test(1, 2));
foreach (var v in values)
System.Diagnostics.Debug.Print(v.ToString());
}
private object[] GetParams<T>(Expression<Func<T>> expr)
{
var body = (MethodCallExpression)expr.Body;
var args = body.Arguments;
return args.Select(p => ((ConstantExpression)p).Value).ToArray();
}
public int Test(int arg1, int arg2)
{
return arg1 + arg2;
}
}