Avatar of Andy Green
Andy Green
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Get property Name not value

I have a class with a property of message.OldLine1.


I need the actual text value of OldLine1, not its value.


I have tried GetType().GetProperty but there is no option for Name.


Andy

C#ASP.NET

Avatar of undefined
Last Comment
Andy Green

8/22/2022 - Mon
AndyAinscow

I'm not so clear what you want.
Do I understand it is the following
eg
class X
Property Name OldLine1
Property value "smith"

you can get "smith" back but you require the name of the property: OldLine1
ASKER CERTIFIED SOLUTION
Éric Moreau

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
it_saige

Agreed with Eric.  You would use nameof.

Proof of concept -
namespace EE_Q29228844
{
    class Program
    {
        static void Main(string[] args)
        {
            var messages = Enumerable.Range(0, 10).Select(x => new Message { OldLine1 = $"OldLine{x}" });
            foreach (var message in messages)
            {
                Console.WriteLine($"{nameof(message.OldLine1)} has a value of {message.OldLine1}");
            }
            Console.ReadLine();
        }
    }

    class Message
    {
        public string OldLine1 { get; set; }
    }
}

Open in new window

Produces the following output -

HTH,

-saige-
Andy Green

ASKER
Thanks Guys. nameof was what I needed.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck