fork download
  1. using System;
  2. using System.Reflection;
  3. using System.Globalization;
  4.  
  5. public class SomeClass
  6. {
  7. public int Range1 { get; set; }
  8. public int Range2 { get; set; }
  9. public int Range3 { get; set; }
  10. public int Range4 { get; set; }
  11. public int Range5 { get; set; }
  12. public int Range6 { get; set; }
  13. }
  14.  
  15. public class FieldInfo_SetValue
  16. {
  17. public static void Main()
  18. {
  19. var myObject = new SomeClass();
  20. Type myType = typeof(SomeClass);
  21.  
  22. for(int i=1; i<=6; i++) {
  23. var FieldName = "Range" + i.ToString();
  24. PropertyInfo pi = myType.GetProperty(FieldName,
  25. BindingFlags.Public | BindingFlags.Instance);
  26.  
  27. pi.GetValue(myObject);
  28.  
  29. pi.SetValue(myObject, i);
  30. Console.WriteLine( "The field value of {0} is \"{1}\".",
  31. FieldName, pi.GetValue(myObject));
  32. }
  33. }
  34. }
  35.  
Success #stdin #stdout 0.04s 23976KB
stdin
Standard input is empty
stdout
The field value of Range1 is "1".
The field value of Range2 is "2".
The field value of Range3 is "3".
The field value of Range4 is "4".
The field value of Range5 is "5".
The field value of Range6 is "6".