Posted By

rtipton on 02/28/10


Tagged

Reflection


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

emregulcan
khouser
jimfred
mheinrich


GetProperties()


 / Published in: C#
 

  1. using System;
  2. using System.Reflection;
  3.  
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. var person = new Person
  9. { Name = "Rhonda",
  10. FaveShow = "Fringe, Lost",
  11. FaveMovie = "Star Trek",
  12. FaveSong = "Contagious" };
  13.  
  14. var type = typeof(Person);
  15. var properties = type.GetProperties();
  16.  
  17. foreach (PropertyInfo property in properties)
  18. {
  19. Console.WriteLine("{0} : {1}", property.Name, property.GetValue(person, null));
  20. }
  21.  
  22. Console.Read();
  23. }
  24. }
  25.  
  26. public class Person
  27. {
  28. public string Name { get; set; }
  29. public string FaveShow { get; set; }
  30. public string FaveMovie { get; set; }
  31. public string FaveSong { get; set; }
  32. }

Report this snippet  

You need to login to post a comment.