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 BindA(int[] v, int idx)
  30. {
  31. return new DelgCell(
  32. delegate(String s) { v[idx] = int.Parse(s); },
  33. delegate() { return v[idx].ToString(); },
  34. delegate() { return typeof(int); }
  35. );
  36. }
  37. static ICell BindM<T>(T v, String name)
  38. {
  39. FieldInfo f = typeof(T).GetField(name);
  40. if (f.FieldType == typeof(int))
  41. {
  42. return new DelgCell(
  43. delegate(String s) { f.SetValue(v, int.Parse(s)); },
  44. delegate() { return f.GetValue(v).ToString(); },
  45. delegate() { return f.FieldType; }
  46. );
  47. }
  48. else { throw new Exception("" + typeof(T)); }
  49. }
  50. class Entry
  51. {
  52. public int n;
  53. }
  54. public static void Main()
  55. {
  56. {
  57. int[] unko = new int[15];
  58. unko[12] = 100;
  59. ICell c = BindA(unko, 12);
  60.  
  61. c.Set("200");
  62. unko[12] += 50;
  63. Console.WriteLine(unko[12] + " " + c.Get() + " " + c.Type()); // "250 250 System.Int32"
  64. }
  65. {
  66. Entry et = new Entry();
  67. et.n = 400;
  68. ICell c = BindM(et, "n");
  69.  
  70. c.Set("500");
  71. et.n += 50;
  72. Console.WriteLine(et.n + " " + c.Get() + " " + c.Type()); // "250 250 System.Int32"
  73. }
  74. }
  75. }
Success #stdin #stdout 0.04s 37112KB
stdin
Standard input is empty
stdout
250 250 System.Int32
550 550 System.Int32