There are 2 classes, different in name, but with the same fields. To transfer data from one class to another, I wrote a method:
public object UpdateValues(object a_fromObject, object a_toObject) { Type _fromType = a_fromObject.GetType(); Type _toType = a_toObject.GetType(); FieldInfo[] _fromFields = _fromType.GetFields(); foreach (FieldInfo _fromField in _fromFields) { FieldInfo _toField = _toType.GetField(_fromField.Name); _toField.SetValue(a_toObject, _fromField.GetValue(a_fromObject)); } return a_toObject; }
Everything would be fine, but if you come across a non-standard type, i.e. for example, another class, it is necessary to recursively call a method, for example, like this:
object _fromFieldObject = _fromField.GetValue(a_fromObject); object _toFieldObject = _toField.GetValue(a_toObject); _toField.SetValue(a_toObject, UpdateValues(_fromFieldObject, _toFieldObject));
Question: What is the best way to define a standard type or derived?