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.