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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:47:40+00:00 2026-05-22T11:47:40+00:00

I have a method and I want to add this method as an extension

  • 0

I have a method and I want to add this method as an extension method to properties of my class.
This method give an expression as input parameter. The method is like below :

    public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
    {
        return (propertyExpression.Body as MemberExpression).Member.Name;
    }

I want to use this method like below example :

string propertyName = MyClass.Property1.GetPropertyName();

Is it possible? if yes, what is the solution?

  • 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-22T11:47:41+00:00Added an answer on May 22, 2026 at 11:47 am

    No, it’s not possible to do that. It’s not clear whether MyClass is the name of a class (and Property1 is a static property) or whether it’s an instance property and MyClass.Property1 simply isn’t a valid member access. If it’s the latter, you probably want to change your method to something like:

    public static string GetPropertyName<TSource, TResult>(
        Expression<Func<TSource, TResult>> propertyExpression)
    {
        return (propertyExpression.Body as MemberExpression).Member.Name;
    }
    

    and call it as:

    string propertyName = GetPropertyName<MyClass, string>(x => x.Property1);
    

    Or you could use a generic class with a generic method, so that string can be inferred:

    string propertyName = PropertyUtil<MyClass>.GetPropertyName(x => x.Property1);
    

    That would be something like:

    public static class PropertyUtil<TSource>
    {
        public static string GetPropertyName<TResult>(
            Expression<Func<TSource, TResult>> propertyExpression)
        {
           return (propertyExpression.Body as MemberExpression).Member.Name;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Title says it mostly. I want to add a simple extension method to the
So, I'd like to hear what you all think about this. I have a
So I want to be able to add/remove class methods at runtime. Before you
I have a situation where i want to add LinePragmas to CodeDom objects. But
According to this post , F# supports extension methods on object instances and static
Ok, Now I want to add a menu in the safari menubar. I Create
I want to use MvcContrib Grid helper, but i stuck on the problem -
I'm beginning to fall in love with Extension Methods, but I just don't know
<disclaimer> What follows is the fruits of a thought experiment. What I'm doing isn't
I am using the Dynamic Linq Library / Sample from Microsoft to do ordering

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.