I have the following method (used to generate friendly error messages in unit tests):
protected string MethodName<TTestedType>(Action<TTestedType> call)
{
return string.Format("{0}.{1}", typeof(TTestedType).FullName, call.Method.Name);
}
But when I call it as follows, I don’t get the expected results:
var nm = MethodName<MyController>(ctrl => ctrl.Create());
After running this code, nm contains "<Create_CreateShowsView>b__8", and not (as expected) "Create". How should I change the code to obtain the expected result?
You need to pass an
Expressioninstead of anAction. It’s actually not that hard to use expression trees for this once you understand what the tree looks like.The line of code:
Can be broken down as as a lambda expression (the entire block), containing one parameter (
c, on the left side), and a body (c.TestMethod(), on the right side).Then the “body” is a method call on a specific object (which is the parameter,
c), an actual method (TestMethod), and a set of arguments (in this case, there aren’t any).Visually:
What you want is the method name, inside the method call expression, inside the body of the lambda expression. So the code to get this is:
Of course, this will only work if the
Expression<Action<T>>passed in is a genuine method call expression; the consumer of this method could technically pass in any expression, in which case this will just returnnull. You can adjust this to throw an exception instead, return a default value, or perform whatever other action you think is appropriate.You don’t need to do anything special to use this – it’s the same usage as your original method: