Within the following method, I would like to access any optional arguments that are contained within the Action:
public static class ValidatorEngine
{
public static void Validate(Action someMethodWithOptionalArguments)
{
object target = someMethodWithOptionalArguments.Target;
}
}
So if I called this method like so:
ValidatorEngine.Validate(() => UpdateByModel(model));
I would like to be able to access the model argument passed into the Action. I’m not even interested in invoking this Action.
I’m thinking that there is something that can be done with the Target property of the Action because I can see the model when debugging. I just can’t figure it out programmatically.
If you want to inspect it but not execute it, this is a prime example for an
Expression. Just change the signature fromActiontoExpression<Action>. This will give you an expression tree that you can analyse. For a basic example:To support a richer set of nodes and scenarios, see this richer version.