C# - How to set a value to a child of a field acquired by Refelctions

Viewed 76

So, I have some objects (various types), that have another objects as fields (also various types). And that objects have let's say a float field. How can I set the last one, since I don't have that 'middle' object which reflection requires?

I'm getting it in that way:

FieldInfo field = foo.GetType().GetField(name1).FieldType.GetField(name2);

And here comes my problem:

field.SetValue(object_that_i_dont_have, float_value);

1 Answers
FieldInfo bfield = foo.GetType().GetField(name1);
FieldInfo field = bfield.FieldType.GetField(name2);
object bar = bfield.GetValue(foo);
field.SetValue(bar, float_value);
Related