如何编写一般方法来复制所有从同一抽象基类继承的类的实例?

时间:2012-08-25 21:02:52

标签: c# silverlight-4.0

我想根据下面的代码将一个画笔实例的细节复制到一个新实例:

private static Brush CopyBrush(Brush b)
        {
            Brush b1 = new Brush();

            // code to copy settings from b to b1

            return b1;
        }

我的难点在于Brush是一个抽象类,所以我无法实例化它。我有许多继承自Brush的不同类,我可以将其中任何一个传递给这个方法。继承类在Brush中都使用相同的属性,因此复制它们不是问题。可以将返回值转换回调用源的继承类型。

3 个答案:

答案 0 :(得分:1)

如果保证所有派生类都具有带有特定签名的构造函数,例如如果没有任何参数,您可以根据其Type实例实例化相应的子类型:

Brush b1 = (Brush)Activator.CreateInstance(b.GetType());

如果无法保证,您需要在Brush类中引入虚拟方法来创建当前类的新实例。子类可以覆盖它以调用合适的构造函数。

更具体地说,您的Brush类可以使用以下方法:

protected virtual Brush CreateBrushInstance()
{
    return (Brush)Activator.CreateInstance(GetType());
}

在任何需要不同构造函数参数的派生类中,重写此方法并使用适当的参数值调用Activator.Create(Type, object[]) method

(这假定CopyBrushBrush类的方法。否则,对internal方法使用publicCreateBrushInstance可见性。)

答案 1 :(得分:1)

案例1:所有Brush派生类都有一个公共默认构造函数,所有属性都有公共设置器。

在这种情况下,一种通用方法就足够了:

static TBrush Copy<TBrush>(this TBrush brush) where TBrush : Brush, new()
{
    return new TBrush() // <- possible due to the `new()` constraint ^^^
           {
               TypeOfHair = brush.TypeOfHair,
               NumberOfHairs = brush.NumberOfHairs,
               …
           };
}

案例2:不符合上述一项或多项前提条件;即没有公共默认构造函数,或者至少有一个属性不可公开设置。

您可以使用此模式:

abstract class Brush
{
    protected abstract Brush PrepareCopy();
    // replacement for public default ctor; prepares a copy of the derived brush
    // where all private members have been copied into the new, returned instance.

    public Brush Copy()
    {
        // (1) let the derived class prepare a copy of itself with all inaccessible
        //     members copied:
        Brush copy = PrepareCopy();

        // (2) let's copy the remaining publicly settable properties:
        copy.TypeOfHair = this.TypeOfHair;
        copy.NumberOfHairs = this.NumberOfHairs;

        return copy;
    } 
}

sealed class FooBrush : Brush
{
    public FooBrush(int somethingPrivate)
    {
        this.somethingPrivate = somethingPrivate;
        // might initialise other members here…
    }

    private readonly int somethingPrivate;

    protected override Brush PrepareCopy()
    {
        return new FooBrush(somethingPrivate);
    }
}

答案 2 :(得分:-1)