Get Argument Values From Linq Expression
If you even find yourself unpacking an expression in C#, you might find this useful. I found myself in need of obtaining a list of argument values from within an Expression<func> expression that sometimes had chained method calls. For example, I needed to be able to get the arguments from all of the following…
SomeMethod(() => getInformation.ForCustomer(CustomerId));
SomeMethod(() => MyFactory.GetInformation().ForCustomer(CustomerId));
SomeMethod(() => MyFactory.GetInformation().ForCustomer(CustomerId).ToList());
In the end, I created a couple of extension methods to help. One converts an expression into a MethodCallExpression (if that is a valid conversion), which is a type that has arguments. The second recursively searches for the arguments.