How to deep copy a class without marking it as Serializable

前端 未结 7 490
自闭症患者
自闭症患者 2021-01-04 00:09

Given the following class:

class A
{
    public List ListB;

    // etc...
}

where B is another class that may inheri

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 00:48

    An answer from a different thread that using json serialization is the best I've seen.

    public static T CloneJson(this T source)
    {      
        if (Object.ReferenceEquals(source, null))
        {
            return default(T);
        }    
        return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(source));
    }
    

提交回复
热议问题