如何克隆从C#中的抽象类派生的类的实例的通用列表?

时间:2011-09-07 20:41:26

标签: c# clone abstract-class generic-list

我有一个基类:abstract class Base和一些派生类:class Derived: Base,所有数据成员都在基类中。我有另一个通用列表:List<Base> list。现在我想在列表上执行Clone()操作。我读过this thread,但发现我的情况有点复杂。由于基类是抽象的,因此列表的元素不能通过复制构造函数或实现ICloneable接口进行克隆。但由于所有数据成员都在基类中,因此一次又一次地编写同一段代码以便在派生类中进行克隆将是多余的。做这份工作的最佳方法是什么?感谢您提供提示。

更新:附加简化的源代码

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;
    }

可以看出两个列表的元素是相同的。

1 个答案:

答案 0 :(得分:4)

我不明白为什么Base无法实现ICloneable

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

当在派生类上调用时,它仍将返回正确的实例(即与现有对象相同的类型)。派生类中的任何字段也将被复制 - 尽管是以浅层方式。作为一个具体的例子:

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());
    }
}

编辑:我怀疑你想要的东西:

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();
    }
}