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

后端 未结 7 1436
忘掉有多难
忘掉有多难 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.

提交回复
热议问题