Avatar ·

How to get the value of an object property by its string name via reflection in C#?

Hello everyone! I'm new to C# and I've encountered a task: I have an object of the Person class, and I want to dynamically get the values of its properties, but the property name is passed as a string. For example, the user enters "Name" or "Age", and I need to return the corresponding value. Here's my class: ```csharp public class Person { public string Name { get; set; } public int Age { get; set; } public string City { get; set; } } ``` I create an object: ```csharp var person = new Person { Name = "Ivan", Age = 30, City = "Moscow" }; string propertyName = "Name"; // or "Age", "City" ``` How can I get the value of person.Name if I only know the string "Name"? I've heard about reflection, but I don't understand how to apply it. I tried something like: ```csharp var value = pers