What is the use of IClonable interface in .NET?

后端 未结 5 1592
时光说笑
时光说笑 2020-12-20 15:34

I just wanted to know what is the use of IClonable interface in .NET?

5条回答
  •  别那么骄傲
    2020-12-20 15:44

    What about defining IShallowClone and IDeepClone like this?

    public interface IShallowClone { T ShallowClone(); }
    public interface IDeepClone { T DeepClone(); }
    
    public interface IA : IShallowClone, IDeepClone { double a { get; } IA ia { get; } }
    public interface IB : IA, IShallowClone, IDeepClone { double b { get; } IB ib { get; } }
    
    public class A : IA
    {
        public double a { get; private set; } 
        public IA ia { get; private set; }
        public A(IA that) { this.a = that.a; this.ia = (ia as IDeepClone).DeepClone(); } 
        public IA DeepClone() { return new A(this); }
        public IA ShallowClone() { return this.MemberwiseClone() as IA; }
    }
    public class B : A, IB 
    { public double b { get; private set; } 
        public IB ib { get; private set; }
        public B(IB that) : base(that) { this.b = that.b; this.ib = (ib as IDeepClone).DeepClone(); } 
        public new IB DeepClone() { return new B(this); }
        public new IB ShallowClone() { return this.MemberwiseClone() as IB; }
    }
    

提交回复
热议问题