Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 709275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:29:09+00:00 2026-05-14T04:29:09+00:00

I have the following method (used to generate friendly error messages in unit tests):

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:29:09+00:00Added an answer on May 14, 2026 at 4:29 am

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method: public string Phase(string phase) { return Phase 1; }
I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
I tried to have Visual Studio 2010 create unit tests for the following class,
I have the following XSL translator that is is being used to generate some
I have following method in wcf webenabled service Public Person AddPerson(Person p); As of
I have following method which I am using to load ActiveX control dynamically, Dim
I have the following method, which takes in the name of a file as
I have the following method that returns void and I need to use it
I have the following method where I want to test the event.status property only
I have the following method in my UIButton class: - (id)initWithCoder:(NSCoder *)aDecoder{ self =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.