Copy an object [Serialize/De-Serialize]

 Scenario:

Copy an object to another one.

Solution:

Serialze to bytes to copy all properties and then De-Serialize to new object.
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
        private static MyNewObject CopyObject(MyObject myObject)
        {
            var data = _serializer.Deserialize<MyNewObject>(_serializer.ToBytes(myObject));
    
            if (data == null) return null;
    
            data.item.ToList().ForEach(li => li.state = "New State");
    
            return data;
        }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...