Link to home
Start Free TrialLog in
Avatar of Andy Green
Andy GreenFlag 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.


Avatar of AndyAinscow
Flag of Switzerland image

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

you can get "smith" back but you require the name of the property: OldLine1
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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}");

    class Message
        public string OldLine1 { get; set; }

Open in new window

Produces the following output -
User generated image

Avatar of Andy Green


Thanks Guys. nameof was what I needed.