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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:15:09+00:00 2026-05-18T12:15:09+00:00

I’m wondering if following code is safe. By safe I mean that I don’t

  • 0

I’m wondering if following code is “safe”. By “safe” I mean that I don’t depend on some specific compiler version or undocumented feature.
I want to get string with property / field name, but I want to declare it using strong typing (I want the compiler to check if specific field / property exists).
My method looks like this:

string GetPropertyName<T>(Expression<Func<T, object>> expression)
{
    if (expression.Body is UnaryExpression)
    {
        var operand = ((UnaryExpression)expression.Body).Operand.ToString();
        return operand.Substring(operand.IndexOf(".") + 1);
    }
    else if (expression.Body is MemberExpression)
    {
        return ((MemberExpression)expression.Body).Member.Name;
    }
    else
    {
        throw new NotImplementedException();
    }            
}

And here is how I want to use it:

class Foo
{
    public string A { get; set; }
    public Bar B { get; set; }
}

class Bar
{
    public int C { get; set; }
    public Baz D { get; set; }
}

class Baz
{
    public int E { get; set; }
}


GetPropertyName<Foo>(x => x.A)
GetPropertyName<Foo>(x => x.B)
GetPropertyName<Foo>(x => x.B.C)
GetPropertyName<Foo>(foo => foo.B.D.E)

Thanks in advance for help.

  • 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-18T12:15:10+00:00Added an answer on May 18, 2026 at 12:15 pm

    I’m not sure that the output of the ToString method is guaranteed in any way. The documentation just says that it “returns a textual representation of the Expression“.

    (I suspect that the output is unlikely to change across different platforms/versions, but I’d be a bit reluctant to rely on it when your aim is to use strong typing, compile-time checks etc.)

    Here’s my method that does something similar without using ToString:

    public static string GetPropertyName<T>(Expression<Func<T, object>> e)
    {
        MemberExpression me;
        switch (e.Body.NodeType)
        {
            case ExpressionType.Convert:
            case ExpressionType.ConvertChecked:
                var ue = e.Body as UnaryExpression;
                me = ((ue != null) ? ue.Operand : null) as MemberExpression;
                break;
            default:
                me = e.Body as MemberExpression;
                break;
        }
    
        if (me == null)
            throw new ArgumentException("Expression must represent field or property access.", "e");
    
        var stack = new Stack<string>();
    
        do
        {
            stack.Push(me.Member.Name);
            me = me.Expression as MemberExpression;
        } while (me != null);
    
        return string.Join(".", stack);    // use "stack.ToArray()" on .NET 3.5
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.