Cloning Windows Forms controls from a template In a recent project I needed a way to clone windows forms controls from a template which had predefined Size, Font, etc.

Since there is no Clone() method in the Control class, I created my own object that does that:

public class ControlTemplate&lt;TSource&gt;
    where TSource : Control
{
    private readonly List&lt;Action&lt;TSource&gt;&gt; _controlActions;

    public ControlTemplate()
    {
        _controlActions = new List&lt;Action&lt;TSource&gt;&gt;();
    }

    public ControlTemplate&lt;TSource&gt; WithArgument(Action&lt;TSource&gt; action)
    {
        if (action == null)
        {
            throw new ArgumentNullException(nameof(action));
        }
        _controlActions.Add(action);
        return this;
    }

    public TSource Clone()
    {
        TSource control = Activator.CreateInstance&lt;TSource&gt;();
        foreach (var action in _controlActions)
        {
            action.Invoke(control);
        }
        return control;
    }
}


It works somewhat like the Builder pattern, you can call WithArgument multiple times and all of the changes to the control will be saved in a list, to be later executed in the Clone method, on a fresh object in order to apply all of the changes.

The problem with this class is that it's not immutable and that's something really important here in order to have the same object every time you clone it. 

If you have an instance of it stored somewhere that works as a template, outside classes can access the Clone method to create new object but they can also make modifications to the template using the WithArgument method, which will cause problems.

In order to fix that I made a "wrapper" class that only exposes the Clone method and operates with an instance of ControlTemplate internally which makes the template more immutable:

public class ControlTemplateCreator&lt;TSource&gt;
    where TSource : Control
{
    private readonly ControlTemplate&lt;TSource&gt; _sourceOfTemplate;

    public ControlTemplateCreator(ControlTemplate&lt;TSource&gt; sourceOfTemplate)
    {
        if (sourceOfTemplate == null)
        {
            throw new ArgumentNullException(nameof(sourceOfTemplate));
        }
        _sourceOfTemplate = sourceOfTemplate;
    }

    public TSource Clone()
    {
        return _sourceOfTemplate.Clone();
    }
}


Example usage would be:

public static class Settings
{
    private static readonly ControlTemplate&lt;Label&gt; _templateLabel;
    public static ControlTemplateCreator&lt;Label&gt; TemplateLabel { get; }

    static Settings()
    {
        _templateLabel = new ControlTemplate&lt;Label&gt;()
            .WithArgument(label =&gt; label.TextAlign = ContentAlignment.MiddleLeft)
            .WithArgument(label =&gt; label.AutoSize = false)
            .WithArgument(label =&gt; label.Size = new Size(135, 20))
            .WithArgument(label =&gt; label.Margin = new Padding(0))
            .WithArgument(label =&gt; label.Font = new Font("Aerial", 9));

        TemplateLabel = new ControlTemplateCreator&lt;Label&gt;(_templateLabel);
    }
}

public class Foo
{
    public void Bar()
    {
        Label clonedLabelFromTemplate = Settings.TemplateLabel.Clone();
    }
}


The code looks pretty good to me, it's extendable, you can create various templates from the same class, outside classes cant modify the original template.

Here are some of my questions:


I wonder why isn't there a Clone method in the Control class are there any bad sides to cloning controls this way?
Are there any flaws in the code?
I'm not sure if WithArgument is a good name.
I think you should remove Clone method from your builder. So you essentially would have:

interface ITemplateBuilder&lt;TSource&gt;
{
     ITemplateBuilder&lt;TSource&gt; WithArgument(...);
     ITemplate&lt;TSource&gt; Build();
}


where:

interface ITemplate&lt;TSource&gt;
{
     TSource Clone();
}


while it is similar to what you have, it conveys your intention in clearer way.

But overall I have to agree with Adriano, I think you are over-engineering. A simple factory will do the job and will probably take less code to write. However you can improve your implementation so it works more like a simplified version of WPF style, so that those actions you were saving up can be applied to any instance of Control. That, I think, would be much more useful. That you can't do with a factory.

 var style = new ControlStyle&lt;Label&gt;()
        .Setter(label =&gt; label.TextAlign = ContentAlignment.MiddleLeft)
        .......;
 style.Apply(_myLabel);
 style.Apply(_myCustomClassThatInheritsLabel);