C# Using Reflection to copy base class properties
- by David Liddle
I would like to update all properties from MyObject to another using Reflection. The problem I am coming into is that the particular object is inherited from a base class and those base class property values are not updated.
The below code copies over top level property values.
public void Update(MyObject o)
{
MyObject copyObject = ...
FieldInfo[] myObjectFields = o.GetType().GetFields(
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
foreach (FieldInfo fi in myObjectFields)
{
fi.SetValue(copyObject, fi.GetValue(o));
}
}
I was looking to see if there were any more BindingFlags attributes I could use to help but to no avail.