Deep Copy a .NET Class Instance Without Serialization

前端 未结 3 709
轻奢々
轻奢々 2021-02-15 16:33

I am using an instance class from a third-party DLL, and I need to do a deep copy on a particular instance. The class is not marked as Serializable, and therefore

3条回答
  •  渐次进展
    2021-02-15 17:02

    One suggestion is to use Json serialization (which uses reflection, and doesn't rely on the [Serializable] attribute) to serialize and deserialize into a copy. For example, using the Json.Net library:

    var copiedObject = JsonConvert.DeserializeObject(
        JsonConvert.SerializeObject(sourceSnapshotObject));
    

提交回复
热议问题