How do I do this in C#?
using System;
namespace TestProperties28373
{
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer { FirstName = "Jim", LastName = "Smith", Age = 34};
Console.WriteLine(customer.FirstName);
string propertyName = "FirstName";
Console.WriteLine(customer.&&propertyName); //PSEUDO-CODE
Console.ReadLine();
}
}
class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
}
Use reflection :
using System.Reflection;
...
PropertyInfo prop = typeof(Customer).GetProperty(propertyName);
object value = prop.GetValue(customer, null);
Use System.Reflection
and PropertyInfo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With