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<T>();
Action<String> setF;
GetDelg<String> getF;
GetDelg<Type> getT;
public DelgCell(Action<String> setF, GetDelg<String> getF, GetDelg<Type> getT)
{
this.setF = setF;
this.getF = getF;
this.getT = getT;
}
}
static ICell Bind<T>(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>(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"
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLlJlZmxlY3Rpb247CgpwdWJsaWMgY2xhc3MgVGVzdAp7CglpbnRlcmZhY2UgSUNlbGwKCXsKCQl2b2lkIFNldChTdHJpbmcgcyk7CgkJU3RyaW5nIEdldCgpOwoJCVR5cGUgVHlwZSgpOwoJfQoJY2x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