fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4.  
  5. public class Test
  6. {
  7. interface Col
  8. {
  9. String Get(Object o);
  10. void Set(Object o, String s);
  11. }
  12. class IntCol : Col
  13. {
  14. FieldInfo f;
  15. public IntCol(FieldInfo f)
  16. {
  17. this.f = f;
  18. }
  19. public String Get(Object o) { return "" + f.GetValue(o); }
  20. public void Set(Object o, String s) { f.SetValue(o, int.Parse(s)); }
  21. }
  22. static Col ToCol(Type t, String name)
  23. {
  24. FieldInfo f = t.GetField(name);
  25. if (f.FieldType == typeof(int)) { return new IntCol(f); }
  26. throw new Exception("" + t);
  27. }
  28. class Table<T> where T : class
  29. {
  30. public List<Col> cols = new List<Col>();
  31. public List<T> rows = new List<T>();
  32.  
  33. public String Get(int row, int col) { return cols[col].Get(rows[row]); }
  34. public void Set(int row, int col, String s) { cols[col].Set(rows[row], s); }
  35.  
  36. public void AddField(String name) { cols.Add(ToCol(typeof(T), name)); }
  37. public void Add(T o) { rows.Add(o); }
  38. }
  39.  
  40. class Entry
  41. {
  42. public int num1;
  43. public int num2;
  44. public Entry(int n1, int n2) { num1 = n1; num2 = n2; }
  45. }
  46. public static void Main()
  47. {
  48. Table<Entry> t = new Table<Entry>();
  49. t.AddField("num1");
  50. t.AddField("num2");
  51.  
  52. t.Add(new Entry(1, 2));
  53. t.Add(new Entry(10, 20));
  54.  
  55. t.Set(1, 1, "300");
  56.  
  57. foreach (Entry e in t.rows)
  58. {
  59. foreach (Col c in t.cols)
  60. {
  61. Console.Write(c.Get(e) + " ");
  62. }
  63. Console.WriteLine();
  64. }
  65. }
  66. }
Success #stdin #stdout 0.04s 37104KB
stdin
Standard input is empty
stdout
1 2 
10 300