How to deep copy a class without marking it as Serializable

前端 未结 7 489
自闭症患者
自闭症患者 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<T>(this T source)
    {      
        if (Object.ReferenceEquals(source, null))
        {
            return default(T);
        }    
        return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source));
    }
    
    0 讨论(0)
提交回复
热议问题