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?

    1 answer 1

    Why reinvent the wheel to display one type to another? "Everything" is already invented. There are dozens of stable mappers. We at work use EmitMapper . Simple and convenient. I advise.

    • Thank. I 'll try) - IVsevolod
    • 2
      The most convenient way is to take a nuget-package from here nuget.org/packages/EmitMapper PS Yes, I advise you to use nuget. - Ivan Navoznov