In principle, it is divided into two accesses to properties. First you get the myClass1Object property, then you set the value property as a result.
Obviously, you will need to take any format in which you have the name of the property and separate it - for example. by points. For example, this should do an arbitrary depth of properties:
public void SetProperty(object source, string property, object target) { string[] bits = property.Split('.'); for (int i=0; i < bits.Length - 1; i++) { PropertyInfo prop = source.GetType().GetProperty(bits[i]); source = prop.GetValue(source, null); } PropertyInfo propertyToSet = source.GetType() .GetProperty(bits[bits.Length-1]); propertyToSet.SetValue(source, target, null); }
Admittedly, you'll probably want a little more error checking than this :)
Jon Skeet Sep 09 '09 at 22:33 2009-09-09 22:33
source share