using System.Collections.Generic;
namespace ReflectionTechnique
static void Main(string[] args)
Customer customer = new Customer("Cust1", "Miami", 123.34m);
Console.WriteLine($"{customer["Name"]} is in {customer["City"]}. The sales are {customer.Sales}");
customer["City"] = "Toledo";
Console.WriteLine($"The city after executing \ncustomer[\"City\"] = \"Toledo\" \nis {customer["City"]}.");
List<Customer> customers = GetCustomers();
DisplaySortedCustomers("Name", customers);
DisplaySortedCustomers("Sales", customers);
DisplaySortedCustomers("City", customers);
public static void DisplaySortedCustomers(string ColumnName, List<Customer> customers)
Console.WriteLine($"\n\nSorted by '{ColumnName}'\n--------------------");
OutputCustomers(SortCustomers(customers, ColumnName));
public static void OutputCustomers(List<Customer> customers)
foreach (var c in customers)
Console.WriteLine($"{c.Name}, {c.City}, {c.Sales}");
public static List<Customer> SortCustomers(List<Customer> customers, string SortByColumn)
return customers.OrderBy(x => x[SortByColumn]).ToList();
public static List<Customer> GetCustomers()
List<Customer> customers = new List<Customer>();
customers.Add(new Customer("Lynn", "Miami", 100.00m));
customers.Add(new Customer("Diana", "West Palm Beach", 200.00m));
customers.Add(new Customer("Brianna", "Chicago", 300.00m));
public class Customer : IndexerProperty
public Customer(string Name, string City, decimal Sales)
public string Name { get; set; }
public string City { get; set; }
public decimal Sales { get; set; }
public class IndexerProperty
public object this[string propertyName]
get { return this.GetType().GetProperty(propertyName).GetValue(this); }
set { this.GetType().GetProperty(propertyName).SetValue(this, value); }