fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4.  
  5. public class Test
  6. {
  7. interface ICell
  8. {
  9. void Set(String s);
  10. String Get();
  11. Type Type();
  12. }
  13. class DelgCell : ICell
  14. {
  15. public void Set(String s) { setF(s); }
  16. public String Get() { return getF(); }
  17. public Type Type() { return getT(); }
  18. public delegate T GetDelg<T>();
  19. Action<String> setF;
  20. GetDelg<String> getF;
  21. GetDelg<Type> getT;
  22. public DelgCell(Action<String> setF, GetDelg<String> getF, GetDelg<Type> getT)
  23. {
  24. this.setF = setF;
  25. this.getF = getF;
  26. this.getT = getT;
  27. }
  28. }
  29. static ICell Bind<T>(T[] v, int arrayIndex)
  30. {
  31. if (typeof(T) == typeof(int))
  32. {
  33. return new DelgCell(
  34. delegate(String s) { (v as int[])[arrayIndex] = int.Parse(s); },
  35. delegate() { return v[arrayIndex].ToString(); },
  36. delegate() { return typeof(int); }
  37. );
  38. }
  39. else { throw new Exception("" + typeof(T)); }
  40. }
  41. static ICell Bind<T>(T v, String fieldName)
  42. {
  43. FieldInfo f = typeof(T).GetField(fieldName);
  44. if (f.FieldType == typeof(int))
  45. {
  46. return new DelgCell(
  47. delegate(String s) { f.SetValue(v, int.Parse(s)); },
  48. delegate() { return f.GetValue(v).ToString(); },
  49. delegate() { return f.FieldType; }
  50. );
  51. }
  52. else { throw new Exception("" + typeof(T)); }
  53. }
  54. class Entry
  55. {
  56. public int n;
  57. public int[] na = new int[15];
  58. }
  59. public static void Main()
  60. {
  61. {
  62. Entry et = new Entry();
  63. et.na[12] = 100;
  64. ICell c = Bind(et.na, 12);
  65.  
  66. c.Set("200");
  67. et.na[12] += 50;
  68. Console.WriteLine(et.na[12] + " " + c.Get() + " " + c.Type()); // "250 250 System.Int32"
  69. }
  70. {
  71. Entry et = new Entry();
  72. et.n = 400;
  73. ICell c = Bind(et, "n");
  74.  
  75. c.Set("500");
  76. et.n += 50;
  77. Console.WriteLine(et.n + " " + c.Get() + " " + c.Type()); // "550 550 System.Int32"
  78. }
  79. }
  80. }
Success #stdin #stdout 0.04s 37088KB
stdin
Standard input is empty
stdout
250 250 System.Int32
550 550 System.Int32