Get Parameters from Action<T>
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>开发者_如何学C 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 particular ResolveMethod
, which returns the values in the out
array (after walking any captured variables, etc).
After making ResolveMethod
public (and removing everything above ResolveMethod
), the code is just:
public static object[] GetParams<T>(this T obj, Expression<Action<T>> action)
{
ProtoClientExtensions.ResolveMethod<T>(
action, out Action ignoreThis, out object[] args);
return args;
}
It should be something like this:
public static object[] GetParams<T>(this T obj, Expression<Action<T>> action)
{
return ((MethodCallExpression) action.Body).Arguments.Cast<ConstantExpression>().Select(e => e.Value).ToArray();
}
you should do some checking to verify that nothing invalid can sent into the action, as not everything is going to cast to a MethodCallExpression, but you should be able to follow from there
Your action x => x.Bar(7, "hello")
can be rewritten as
void action(T x)
{
return x.Bar(7, "hello");
}
It's clear now that 7
and "hello"
are not the parameters of the action, only x
is.
In order to get access to 7
and "hello"
, you need an access to the expression, like what @Marc suggests. However it's not clear how your code should handle more complicated expressions, like x => 1 + x.Bar(7, x.Baz("hello", x.Quux(Application.Current)))
.
精彩评论