Duplicate Object and working with Duplicate without changing Original

后端 未结 8 648
逝去的感伤
逝去的感伤 2020-12-08 14:02

Assuming I have an Object ItemVO in which there a bunch of properties already assigned. eg:

ItemVO originalItemVO = new ItemVO();
originalItemVO.ItemId = 1;         


        
8条回答
  •  猫巷女王i
    2020-12-08 14:41

    As of c# 4.5 the base class object contains a method called MemberwiseClone that enables you to perform a shallow copy of that object and returns the result as a new instance. (If a field is a value type, a bit-by-bit copy of the field is performed. If a field is a reference type, the reference is copied but the referred object is not; therefore, the original object and its clone refer to the same object.)

    This is useful if you are looking to implement a prototype design pattern.

    If you are looking to implement a deep copy (everything within the class is duplicated as new instances) then serialization or reflection are probably the best tools

提交回复
热议问题