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.


Andy

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
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}");
            }
            Console.ReadLine();
        }
    }

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

Open in new window

Produces the following output -
User generated image
HTH,

-saige-
Avatar of Andy Green

ASKER

Thanks Guys. nameof was what I needed.