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 Bind(T[] v, int arrayIndex) { if (typeof(T) == typeof(int)) { return new DelgCell( delegate(String s) { (v as int[])[arrayIndex] = int.Parse(s); }, delegate() { return v[arrayIndex].ToString(); }, delegate() { return typeof(int); } ); } else { throw new Exception("" + typeof(T)); } } static ICell Bind(T v, String fieldName) { FieldInfo f = typeof(T).GetField(fieldName); 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 int[] na = new int[15]; } public static void Main() { { Entry et = new Entry(); et.na[12] = 100; ICell c = Bind(et.na, 12); c.Set("200"); et.na[12] += 50; Console.WriteLine(et.na[12] + " " + c.Get() + " " + c.Type()); // "250 250 System.Int32" } { Entry et = new Entry(); et.n = 400; ICell c = Bind(et, "n"); c.Set("500"); et.n += 50; Console.WriteLine(et.n + " " + c.Get() + " " + c.Type()); // "550 550 System.Int32" } } }