Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编写一个通用方法来复制所有继承自同一抽象基类的类的实例?_C#_Silverlight 4.0 - Fatal编程技术网

C# 如何编写一个通用方法来复制所有继承自同一抽象基类的类的实例?

C# 如何编写一个通用方法来复制所有继承自同一抽象基类的类的实例?,c#,silverlight-4.0,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继承了许多不同的类,我

我想按照下面的代码将笔刷的一个实例的详细信息复制到一个新实例:

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

            // code to copy settings from b to b1

            return b1;
        }
我的困难在于Brush是一个抽象类,所以我无法实例化它。我从Brush继承了许多不同的类,我可以将它们中的任何一个传递给这个方法。内在类在笔刷中都使用相同的属性,所以复制它们不是问题。可以将返回转换回调用源处的继承类型。

尝试克隆方法


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

Brush b1 = (Brush)Activator.CreateInstance(b.GetType());
如果不能保证这一点,则需要在
Brush
类中引入虚拟方法来创建当前类的新实例。子类可以重写它以调用合适的构造函数

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

protected virtual Brush CreateBrushInstance()
{
    return (Brush)Activator.CreateInstance(GetType());
}
在任何需要不同构造函数参数的派生类中,重写此方法并使用适当的参数值调用


(这假设
CopyBrush
Brush
类的一个方法。否则,对
CreateBrushInstance
方法使用
internal
public
可见性。)

案例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,
               …
           };
}

Brush
是您自己定义的BCL类还是自定义类?Brush是我自己定义的类问题中没有任何内容指向所提到的
Brush
类具有
Clone
方法。另外,
System.Drawing.Brush
不在Silverlight中。请注意,从问题代码的角度来看,案例1中的代码的行为可能与直觉相反
TBrush
必须是
brush
实例的确切类型,而不是其基本类型,因为泛型类型参数
TBrush
将被解析,并且必须在编译时已知。特别是,类似于
Brush myBrush=new SpecialBrush()的调用;var copyOfMyBrush=myBrush.Copy()
不会创建类型为
SpecialBrush
的新实例,而是类型为
Brush
的新实例。
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);
    }
}