Reputation: 15
Hey i was wondering if i can convert an Expression to an Action. I need to use the Expression to get the details of the lambda expression and at the same time i need to execute it using a different method. I need to get the Expression and the actual action with just using a single parameter (either Action or Expression): BTW i need this for Getting details on what kind of assert i did. ex(Assert.true, Assert.False)
public void otherMethod()
{
SomeMethod(() => Assert.Equals("Dog","Cat"));
}
public void SomeMethod(Expression<Action> neededAction) //or public void SomeMethod(Action neededAction)
{
//i need to run the neededAction and get the details whether what assert i did and the inputs i used for the assertion
}
So basically i need to run the Action and i need to get its method infos. Thanks~
Upvotes: 1
Views: 852
Reputation: 15579
You need to call Compile()
on the expression.
// Compile it.
var actualNeededAction = neededAction.Compile();
// Execute it.
actualNeededAction();
Upvotes: 1