fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5.  
  6. public class Test
  7. {
  8. public class Person
  9. {
  10. public string Title{get;set;}
  11. public string Name{get;set;}
  12. public Int32 Age{get;set;}
  13. }
  14.  
  15. public static void Main()
  16. {
  17. List<String> fields = new List<String>()
  18. {
  19. "Title",
  20. "Age"
  21. };
  22.  
  23. var persons = new List<Person>();
  24. persons.Add(new Person { Title = "A1", Age = 10 });
  25. persons.Add(new Person { Title = "A2", Age = 20 });
  26. persons.Add(new Person { Title = "A3", Age = 30 });
  27.  
  28. //Populate persons
  29.  
  30. IEnumerable<PropertyInfo> properties = typeof(Person)
  31. .GetProperties(BindingFlags.Public | BindingFlags.Instance)
  32. .Where(p => fields.Contains(p.Name));
  33.  
  34. foreach (Person person in persons)
  35. {
  36. foreach (PropertyInfo prop in properties)
  37. Console.WriteLine("{0}: {1}", prop.Name, prop.GetValue(person, null));
  38. }
  39. }
  40. }
Success #stdin #stdout 0.04s 37168KB
stdin
Standard input is empty
stdout
Title: A1
Age: 10
Title: A2
Age: 20
Title: A3
Age: 30