How to deep copy a class without marking it as Serializable

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

        private interface IDeepCopy where T : class
        {
            T DeepCopy();
        }
    
        private class MyClass : IDeepCopy
        {
            public MyClass DeepCopy()
            {
                return (MyClass)this.MemberwiseClone();
            }
        }
    

    Pluss: Yoy can control copy process (if your class has identifier property you can set them, or you can write other business logic code)


    Minus: class can be marked as sealed


提交回复
热议问题