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

  • Home
  • SEARCH
  • 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 6040569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:31:58+00:00 2026-05-23T06:31:58+00:00

I looked and everywhere are many examples how to do property name resolution, but

  • 0

I looked and everywhere are many examples how to do property name resolution, but I didn’t find that would solve my usage.

My idea of User looks like this:

class Entity<T> where T : class
{
    public static String GetName<T>(Expression<Func<T, object>> expr)
    {
        return ((MemberExpression)expr.Body).Member.Name;
    }
}

class User : Entity<User>
{
    public String UserName { get; set; }
    public DateTime LastLoggedOn { get; set; }
}

Question: How to implement property name resolution if I want to use it like this?

Debug.Assert("UserName" == User.GetField(x => x.UserName));
Debug.Assert("LastLoggedOn" == User.GetField(x => x.LastLoggedOn));

Any help would be appreciated. Thanks.

Notes: I could do var u = new User(); and then u.GetName(() => u.UserName) but in my case, I don’t have an instance of the entity

EDIT 1: Thanks to Darin, I updated my code. I need to get LastLoggedOn work too.

Debugging shows, that value of expr is {x => Convert(x.LastLoggedOn)} (don’t know what the Convert means)

InvalidCastException was unhandled
  Unable to cast object of type 'System.Linq.Expressions.UnaryExpression' to type 'System.Linq.Expressions.MemberExpression'.

EDIT 2/Answer: After some debugging I’ve composed this “solution”. I don’t like it, but it seems to work.

public static string GetName(Expression<Func<T, object>> expression)
{
    MemberExpression memberExp = expression.Body as MemberExpression;
    if (memberExp != null)
        return memberExp.Member.Name;

    // for DateTime
    UnaryExpression unaryExp = expression.Body as UnaryExpression;
    if (unaryExp != null)
    {
        memberExp = unaryExp.Operand as MemberExpression;
        if (memberExp != null)
            return memberExp.Member.Name;
    }

    throw new ArgumentException("'expression' should be a member expression or a method call expression.", "expression");
}
  • 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-23T06:31:59+00:00Added an answer on May 23, 2026 at 6:31 am

    Just remove the <T> from the GetName static method and you are good to go (the compiler should have warned you about this by the way):

    public class Entity<T> where T : class
    {
        public static string GetName(Expression<Func<T, object>> expr)
        {
            return ((MemberExpression)expr.Body).Member.Name;
        }
    }
    
    public class User: Entity<User>
    {
        public String UserName { get; set; }
    }
    

    Now you can write:

    string name = User.GetName(x => x.UserName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have looked everywhere to find a linux utility that will allow me to
I have looked everywhere but couldn't find it anywhere. I installed it successfully but
I've looked everywhere for the answer to this question but cant find anything so
I've looked everywhere for this but can't find an answer, so: I have a
I've looked everywhere but I can't find where SummaryReporter writes the tests report? If
So I looked everywhere and I have no idea how to solve this certificate
I've looked everywhere but I can't find any way to post code to GitHub.
I looked at SQL Server dateformat codes but I couldn't find dd.mm.yyyy hh:mm format
I've looked everywhere for this function and cannot find the header files to make
I've looked everywhere for an elegant solution. The essential problem seems to be that

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.