How do I get the parameters passed into an Action<T> ? The code example should highlight what I’m trying to achieve. Sorry that it’s a little bit long.
public class Program
{
public static void Main(string[] args)
{
var foo = new Foo();
foo.GetParams(x => x.Bar(7, "hello"));
}
}
public class Foo
{
public void Bar(int val, string thing) { }
}
public static class Ex
{
public static object[] GetParams<T>(this T obj, Action<T> action)
{
// Return new object[] { 7, "hello" }
}
}
The only options that look vaguely useful are GetInvocationList(), Method and Target. But none of them seem to contain the data I’m after (I think it’s because of the way I’ve declared the Action). Thanks
EDIT: It’s not the types I want, it’s the actual values – as noted in the commented bit of code.
To do that, it should actually be an
Expression<Action<T>>. Then it is a case of decomposing the expression. Fortunately I have all the code for that over in protobuf-net, here – in particularResolveMethod, which returns the values in theoutarray (after walking any captured variables, etc).After making
ResolveMethodpublic (and removing everything aboveResolveMethod), the code is just: