C# - copying property values from one instance to another, different classes

后端 未结 7 1427
忘掉有多难
忘掉有多难 2020-12-10 09:18

I have two C# classes that have many of the same properties (by name and type). I want to be able to copy all non-null values from an instance of Defect into a

相关标签:
7条回答
  • 2020-12-10 10:01

    Replace your erroneous line with this:

    PropertyInfo targetProperty = defectViewModel.GetType().GetProperty(defectProperty.Name);
    targetProperty.SetValue(viewModel, defectValue, null);
    

    Your posted code is attempting to set a Defect-tied property on a DefectViewModel object.

    0 讨论(0)
提交回复
热议问题