views:

70

answers:

2

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?

A: 

That b__8 thing is the name of the method that is generated by the C# compiler for your lambda. You can see this by using Reflector, for instance.

If you need it to say "Create", you'll have to actually make a method named Create. And of course it needs to fit into an Action, so it will have to return void.


In simple cases, you could create a method that takes a string indicating a method to be called, and return an action that calls that method on an object of a specified type. See for instance this tutorial.

Mark Rushakoff
OK. Is there any way I can get the name *I* chose in the code instead of the CLR name of the method?
Tomas Lycken
@Tomas: It's an anonymous function, so it has no name. The method isn't actually `Create`, it's "an unnamed method that takes a `TTestedType` as a parameter and calls its `Create` method."
Aaronaught
@Aaron - you're right, of course. That was something I had overlooked... Is there any way I can get to "Create" from that anonymous method?
Tomas Lycken
I'm no reflection expert in C#, but you should be able to pass in the string "Create" and use reflection to create an action of form `x => x.Create()`. We'll see what the other answers come up with.
Mark Rushakoff
If not, how do I create a named method? I tried assigning the lambda expression to a variable named Create, but still got the wierd b__8 stuff...
Tomas Lycken
@Mark: The reason this problem came up was that I wanted a strongly typed way of getting the method name - I wouldn't need this method at all if passing the string "Create" around was an option... :P
Tomas Lycken
@Tomas: You need to use expression trees, instead...
Reed Copsey
@Reed: OK... Never used them before. Do you have a good link to a quick-start-place? =)
Tomas Lycken
@Tomas: If you take an `Expression<Action<TTestedType>>` instead of an `Action<TTestedType>` then you can parse it as a `LambdaExpression`, with the `Body` being a `MethodCallExpression`, from which you can get the "real" `Method`.
Aaronaught
@Aaronaught: Sounds cool. But I have no clue how to do it... Care to show me?
Tomas Lycken
+4  A: 

You need to pass an Expression instead of an Action. It's actually not that hard to use expression trees for this once you understand what the tree looks like.

The line of code:

(MyClass c) => c.TestMethod();

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:

            LambdaExpression   [ (MyClass c) => c.TestMethod() ]
             /           \
            /             \
           /               \
      Parameters          Body   [ MethodCallExpression: c.TestMethod() ]
          |              /    \
          |             /      \
    1: MyClass c    Object [c]  \
                                /\
                               /  \
                              /    \
             Method [TestMethod]  Arguments [Empty]

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:

static string GetInnerMethodName<T>(Expression<Action<T>> expr)
{
    MethodCallExpression mce = expr.Body as MethodCallExpression;
    return (mce != null) ? mce.Method.Name : null;
}

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 return null. 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:

string methodName = GetInnerMethodName<MyClass>(c => c.TestMethod());
Aaronaught
Thanks a lot for a GREAT answer! =)
Tomas Lycken