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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:57:43+00:00 2026-05-26T21:57:43+00:00

A string representation of an object instance property can be taken with Expression<Func<T>> :

  • 0

A string representation of an object instance property can be taken with Expression<Func<T>>:

string propertyName = ((MemberExpression) property.Body).Member.Name;

But what if I don’t have (don’t want to create) the instance? How do I get the property name in this case?

Explained

I need a string representation of a property name of some object.

Let’s say there is an entity

public class Customer
{
    public int ID;
    public string Name;
}

Now I want to pass the key expression of this entity to some other function, thus I need the string “ID”, but I don’t want to hardcode the string like SomeOtherFunction("ID"), instead I use the expression SomeOtherFunction(ExpressionReader.GetString(() => CustomerInstance.ID)). For this to work I need to supply the entity instance.

Now I want to do the same without creating the instance.

  • 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-26T21:57:43+00:00Added an answer on May 26, 2026 at 9:57 pm

    This is possible with a method signature like the following:

    private static string GetPropertyName<TModel, TProperty>(Expression<Func<TModel, TProperty>> property)
    {
        MemberExpression memberExpression = (MemberExpression)property.Body;
    
        return memberExpression.Member.Name;
    }
    

    You can call this method without an instance of the customer class:

    string propertyName = GetPropertyName((Customer c) => c.ID);
    

    Of course you should add some checks for correct expression types before you cast to MemberExpression and access memberExpression.Member.Name.

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

Sidebar

Related Questions

jQuery.parseJSON('{"name":"John"}') converts string representation to object but I want the reverse. Object is to
My last question was about getting the string representation of an object serialized to
Is there a easy way to convert a DateTime object to a string representation
Consider this class: class foo(object): pass The default string representation looks something like this:
I understand how I can provide an informal representation of an instance of the
In Common Lisp, how can I override the default string representation of a CLOS
I am trying to create a JSON string representation of a class instance and
I have a string representation of an array: '[item1, item2, item3]' I'm trying to
I have a string representation of a MD5 hex digest for a file, that
I would like to take a string representation of a set and parse it.

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.