Cloning objects without Serialization

前端 未结 3 457
傲寒
傲寒 2020-12-02 02:21

I\'ve found numerous solutions here at SO and elsewere that deal with deep clone of object via serialization/deserialization (into memory and back).

It requires that

3条回答
  •  一个人的身影
    2020-12-02 02:29

    Json serialization and deserialization should work, it doesn't require the classes to have serialize annotation.

    public static T DeepCopy(this T source)
    {
        return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(source));
    }
    

提交回复
热议问题