Merge the state of two objects in C# using reflection
if (fromObject == null || toObject == null) { return; } PropertyInfo[] properties = typeof(T).GetProperties(); object value = null; for (int i = 0; i < properties.Length; i++) { if (!properties[i].CanWrite) { continue; } value = null; IList list = properties[i].GetValue(fromObject, null) as IList; if (list != null) { //Is a list. value = list; } else { value = properties[i].GetValue(fromObject, null); } if (doNotMergeIfValueNull && value == null) { //do nothing. } else { properties[i].SetValue(toObject, value, null); } }