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

后端 未结 7 1429
忘掉有多难
忘掉有多难 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 09:52

    For one thing I would not place that code (somewhere) external but in the constructor of the ViewModel:

    class DefectViewModel
    {
        public DefectViewModel(Defect source)  { ... }
    }
    

    And if this is the only class (or one of a few) I would not automate it further but write out the property assignments. Automating it looks nice but there may be more exceptions and special cases than you expect.

提交回复
热议问题