Stefan,
Unfortunately this would not work, because in order to drill down to the
nested object that needs updating, I would need to call:
parent = fi.GetValue (parent) // see code below
which returns me a copy-by-value of each nested level, and SetValue() would
only update this copy, not my original object.
I need something like ref, but applied to GetValue().
The code below supports array children at any level.
I can hardly believe that I need code like this to be able to access
Child[12].Field of Parent using reflection, but I can live with it.
For setting the value of Child[12].Field of Parent I have no solution.
I can feel in my toes that GetValueDirect() and its TypedReference are what
I need, but I can not find any clear info and/or samples on using it.
Thanks.
Michel.
Code:
private static object GetFieldValue (object parent, string field)
{
string[] fields = field.Split ('.');
string fld = "";
FieldInfo fi = null;
Match match = null;
int index = -1;
for (int i = 0; i < fields.Length; i++)
{
fld = fields[i];
match = Regex.Match (fld, @"\[\d*\]");
if (match.Length > 0)
{
fld = fld.Substring (0, match.Index);
index = int.Parse (Regex.Replace (match.ToString(),
@"\[|\]", ""));
}
fi = parent.GetType().GetField (fld);
if (fi != null)
{
parent = fi.GetValue (parent);
if (parent is System.Array)
{
parent = ((System.Array) parent).GetValue (index);
}
}
else
{
parent = null;
}
}
return parent;
}