How to call a property of an object with a variable in C# e.g. customer.&fieldName
<p>In C# is there a way you can call a property of an object with a variable, something like this:</p>

<pre><code>string fieldName = "FirstName";
Console.WriteLine(customer.&amp;fieldName);
</code></pre>

<h2>ANSWER:</h2>

<p>Excellent, thanks for the quick answers, this is what I was trying to do:</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace TestLinqFieldIndex
{
    class Program
    {
        static void Main(string[] args)
        {
            List&lt;Customer&gt; customers = new List&lt;Customer&gt;();
            customers.Add(new Customer { ID = 1, FirstName = "Jim", LastName = "Smith" });
            customers.Add(new Customer { ID = 2, FirstName = "Joe", LastName = "Douglas" });
            customers.Add(new Customer { ID = 3, FirstName = "Jane", LastName = "Anders" });

            var customer = (from c in customers
                            where c.ID == 2
                            select c).SingleOrDefault();

            string[] fieldNames = { "FirstName", "LastName" };
            foreach (string fieldName in fieldNames)
            {
                Console.WriteLine("The value of {0} is {1}.", fieldName, customer.GetPropertyValue(fieldName));
            }

            Console.ReadLine();
        }
    }

    public class Customer
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public string GetPropertyValue(string fieldName)
        {
            PropertyInfo prop = typeof(Customer).GetProperty(fieldName);
            return prop.GetValue(this, null).ToString();
        }

    }
}
</code></pre>
