Search code examples
c#cloneabstract-classgeneric-list

How to clone a generic list of instances of a class derived from an abstract class in C#?


I have a base class: abstract class Base and some derived classes: class Derived: Base, that all data members are in the base class. I have another generic list: List<Base> list. Now I want to perform the Clone() operation on the list. I've read this thread but found that my situation a bit more complex. Since the base class is abstract, so the elements of the list cannot be clone via copy constructor or implementing ICloneable interface. But since all data members are in base class, it will be redundant to write the same piece of code for cloning in derived classes again and again. What is the best way to do this job? Thank you for giving hints.

Update: Simplified Source Code Attached

public class Point : ICloneable
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point(int x, int y)
    { X = x; Y = y; }

    public Point(Point p)
    {
        X = p.X; Y = p.Y;
    }

    public object Clone()
    {
        return MemberwiseClone();
    }
}

public abstract class ObjectThatHasPosition : ICloneable
{
    public Point CurrentPosition { get; set; }
    public ObjectThatHasPosition(Point p)
    { CurrentPosition = new Point(p); }
    public object Clone()
    {
        return MemberwiseClone();
    }
}

public class Man : ObjectThatHasPosition
{
    public Man(Point p) : base(p) { }
}

static class Extensions
{
    public static List<ObjectThatHasPosition> Clone(this List<ObjectThatHasPosition> src)
    {
        List<ObjectThatHasPosition> dst = new List<ObjectThatHasPosition>(src.Count);
        src.ForEach((item) => { dst.Add((ObjectThatHasPosition)item.Clone()); });
        return dst;
    }
}

    static void Main(string[] args)
    {
        List<ObjectThatHasPosition> firstList = new List<ObjectThatHasPosition>();
        firstList.Add(new Man(new Point(0, 0)));
        List<ObjectThatHasPosition> anotherList = firstList.Clone();
        firstList[0].CurrentPosition.X = 1;
    }

One can see that both lists' element is the same.


Solution

  • I don't see why Base can't implement ICloneable:

    public abstract class Base : ICloneable
    {
        public object Clone()
        {
            return MemberwiseClone();
        }
    }
    

    That will still return the right instance (i.e. the same type as the existing object) when called on a derived class. Any fields in the derived classes will be copied over too - albeit in a shallow way. As a concrete example:

    using System;
    
    public abstract class BaseClass : ICloneable
    {
        public object Clone()
        {
            return MemberwiseClone();
        }
    }
    
    public class Derived : BaseClass
    {
        private readonly string name;
    
        public Derived(string name)
        {
            this.name = name;
        }
    
        public override string ToString()
        {
            return "Derived with name " + name;
        }
    }
    
    class Test
    {
        static void Main(string[] args)
        {
            BaseClass b = new Derived("fred");
    
            object clone = b.Clone();
            Console.WriteLine(clone.ToString());
        }
    }
    

    EDIT: I suspect you want something like:

    public abstract class ObjectThatHasPosition : ICloneable
    {
        public Point CurrentPosition { get; set; }
        public ObjectThatHasPosition(Point p)
        {
            CurrentPosition = p; 
        }
    
        public object Clone()
        {
            var clone = (ObjectThatHasPosition) MemberwiseClone();
            clone.CurrentPosition = (Point) CurrentPosition.Clone();
        }
    }