How to deep copy a class without marking it as Serializable

前端 未结 7 487
自闭症患者
自闭症患者 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:40

    your interface IDeepCopy is exactly what ICloneable specifies.

    class B : ICloneable
    {
         public object Clone() { return new B(); }
    }
    

    and with more friendly implementation :

    class B : ICloneable
    {
         public B Clone() { return new B(); }
         // explicit implementation of ICloneable
         object ICloneable.Clone() { return this.Clone(); }
    }
    

提交回复
热议问题