using System; using System.Collections.Generic; using System.Reflection; public class Test { interface ICell { void Set(String s); String Get(); Type Type(); } class DelgCell : ICell { public void Set(String s) { setF(s); } public String Get() { return getF(); } public Type Type() { return getT(); } public delegate T GetDelg(); Action setF; GetDelg getF; GetDelg getT; public DelgCell(Action setF, GetDelg getF, GetDelg getT) { this.setF = setF; this.getF = getF; this.getT = getT; } } static ICell BindA(int[] v, int idx) { return new DelgCell( delegate(String s) { v[idx] = int.Parse(s); }, delegate() { return v[idx].ToString(); }, delegate() { return typeof(int); } ); } static ICell BindM(T v, String name) { FieldInfo f = typeof(T).GetField(name); if (f.FieldType == typeof(int)) { return new DelgCell( delegate(String s) { f.SetValue(v, int.Parse(s)); }, delegate() { return f.GetValue(v).ToString(); }, delegate() { return f.FieldType; } ); } else { throw new Exception("" + typeof(T)); } } class Entry { public int n; } public static void Main() { { int[] unko = new int[15]; unko[12] = 100; ICell c = BindA(unko, 12); c.Set("200"); unko[12] += 50; Console.WriteLine(unko[12] + " " + c.Get() + " " + c.Type()); // "250 250 System.Int32" } { Entry et = new Entry(); et.n = 400; ICell c = BindM(et, "n"); c.Set("500"); et.n += 50; Console.WriteLine(et.n + " " + c.Get() + " " + c.Type()); // "250 250 System.Int32" } } }