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 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>(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"
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLlJlZmxlY3Rpb247CgpwdWJsaWMgY2xhc3MgVGVzdAp7CglpbnRlcmZhY2UgSUNlbGwKCXsKCQl2b2lkIFNldChTdHJpbmcgcyk7CgkJU3RyaW5nIEdldCgpOwoJCVR5cGUgVHlwZSgpOwoJfQoJY2xhc3MgRGVsZ0NlbGwgOiBJQ2VsbAoJewoJCXB1YmxpYyB2b2lkIFNldChTdHJpbmcgcykgeyBzZXRGKHMpOy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