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 8151223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:18:36+00:00 2026-06-06T15:18:36+00:00

I have a method which i want to convert to Extension Method public static

  • 0

I have a method which i want to convert to Extension Method

public static string GetMemberName<T>(Expression<Func<T>> item)
{
    return ((MemberExpression)item.Body).Member.Name;
}

and calling it like

string str = myclass.GetMemberName(() => new Foo().Bar); 

so it evaluates to str = "Bar"; // It gives the Member name and not its value

Now when i try to convert this to extension method by this

public static string GetMemberName<T>(this Expression<Func<T>> item)
{
    return ((MemberExpression)item.Body).Member.Name;
}

and call it like

string str = (() => new Foo().Bar).GetMemberName();

Error says Operator '.' cannot be applied to operand of type 'lambda expression'

Where am I wrong?

  • 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-06-06T15:18:39+00:00Added an answer on June 6, 2026 at 3:18 pm

    There are really two things here, first, passing () => new Foo().Bar into the method that accepts Expression<Func<T>> treats the specified expression tree as a Expression<Func<T>>, but () => new Foo().Bar is not an Expression<Func<T>> on its own.

    Second, in order to get your extension method to accept any lambda (such as you’re supplying), you’d have to use the type that corresponds to any expression tree. But, as you may have already guessed based on the message ... to operand of type 'lambda expression' where you’d usually see the name of the type inside the quotes, that lambda expressions are treated specially by the language, making what you’re trying to do, without casting first, impossible.

    The way to invoke your extension method in extension method form would be (in the case that Bar is of type string)

    ((Expression<Func<string>>)(() => new Foo().Bar)).GetMemberName()` 
    

    which doesn’t seem like it would be all that desirable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method which takes String argument. In some cases I want to
I want my classes X and Y to have a method f(x) which calls
I have an iPhone application which in which I want to perform a method
Hi I want to create a WCF service that have login method, which is
I have a method which returns single word as a String. I need to
I want to have an extension method for XElement/XAttribute that allows me to apply
i want to return a string value from an anonymous method/delegate. How can i
i have a jquery function which i want to convert to .live because i
I have a response object which I want to convert to Json, but somehow
i have a long running function¹: public string FindPasswordFromHash(String hash) { ... } which

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.