title: Cloning WPF controls and object hierarchies
body: <p>I have some problems cloning an object hierarchie. It's a toolkit for modelling applications, the toolbox contains class instances as prototypes. But I'm having a hard time cloning these :)</p>
  
  <p>The following code shows the problem:</p>
  
  <pre><code>public abstract class Shape {
    protected List&lt;UIElement&gt; elements;
    private Canvas canvas;
    ...
    public Canvas getCanvas() { ... };
  }
  
  public class MovableShape : Shape {
    protected ... propertyA;
    private ... propertyXY;
    ...
  }
  
  public abstract class AbstractLayout : MovableShape, ... {
    ...
  }
  
  public class SomeLayoutClass : AbstractLayout, ... {
    ...
  }
  
  public class AContainingClass {
    SomeLayoutClass Layout { get; set; }
    ...
  }
  </code></pre>
  
  <p>When I insert an object of <code>AContainingClass</code> into my project worksheet, it should be cloned. So far I tried manual cloning (which fails because of the <code>private</code> fields in the base classes) and binary serialization (<code>BinaryFormatter</code> and <code>MemoryStreams</code>).</p>
  
  <p>The first approach lacks a way to call the <code>base.clone()</code> method (or am I wrong?), the latter does not work because <code>UIElement</code>s aren't <code>[Serializable]</code>.</p>
  
  <p>Note: it must be a deep copy!</p>
  
  <p>Any ideas? Thanks!</p>
  
  <p><hr>
  UPDATE</p>
  
  <p>Just to clarify my manual cloning approach:
  If each class has it's own <code>Clone</code> method, how to call the <code>Clone</code> method of the base class?</p>
  
  <pre><code>public class Shape { // not abstract any more
    ...
    public Shape Clone() {
      Shape clone = new Shape() { PropertyA = this.PropertyA, ... };
      ...do some XamlWriter things to clone UIElements...
      return clone;
    }
  }
  
  public class MovableShape : Shape {
    ...
    public MovableShape Clone() {
       // how to call base.Clone??? 
       // this would be required because I have no access to the private fields!
    }
  }
  </code></pre>