Open sandboxFocusImprove this doc

Clone example, step 1: getting started

This article will create the first working version of the Cloneable aspect. Once it is done, it will implement the Deep Clone pattern as shown below:

Source Code


1[Cloneable]
2internal class Game
3{

4    public Player Player { get; set; }
5
6    [Child]
7    public GameSettings Settings { get; set; }











8}
Transformed Code
1using System;
2
3[Cloneable]
4internal class Game
5: ICloneable
6{
7    public Player Player { get; set; }
8
9    [Child]
10    public GameSettings Settings { get; set; }
11public virtual Game Clone()
12    {
13        var clone = (Game)this.MemberwiseClone();
14        clone.Settings = ((GameSettings)this.Settings.Clone());
15        return clone;
16    }
17
18    object ICloneable.Clone()
19    {
20        return Clone();
21    }
22}
Source Code


1[Cloneable]
2internal class GameSettings
3{

4    public int Level { get; set; }
5
6    public string World { get; set; }










7}
Transformed Code
1using System;
2
3[Cloneable]
4internal class GameSettings
5: ICloneable
6{
7    public int Level { get; set; }
8
9    public string World { get; set; }
10public virtual GameSettings Clone()
11    {
12        var clone = (GameSettings)this.MemberwiseClone();
13        return clone;
14    }
15
16    object ICloneable.Clone()
17    {
18        return Clone();
19    }
20}

Before we start writing the aspect, we must materialize in C# the concept of a child property. Conceptually, a child property is a property that points to a reference-type object that needs to be cloned when the parent object is cloned. Let's decide to mark such properties with the [Child] custom attribute:

1[AttributeUsage( AttributeTargets.Field | AttributeTargets.Property )]
2public sealed class ChildAttribute : Attribute { }

Aspect implementation

The whole aspect implementation is here:

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4[Inheritable]
5[EditorExperience( SuggestAsLiveTemplate = true )]
6public class CloneableAttribute : TypeAspect
7{
8    public override void BuildAspect( IAspectBuilder<INamedType> builder )
9    {
10        // 
11        builder.Advice.ImplementInterface(
12            builder.Target,
13            typeof(ICloneable),
14            OverrideStrategy.Ignore );
15
16        // 
17
18        // 
19        builder.Advice.IntroduceMethod(
20            builder.Target,
21            nameof(this.CloneImpl),
22            whenExists: OverrideStrategy.Override,
23            args: new { T = builder.Target },
24            buildMethod: m => m.Name = "Clone" );
25
26        // 
27    }
28
29    [InterfaceMember( IsExplicit = true )]
30    private object Clone() => meta.This.Clone();
31
32    [Template]
33    public virtual T CloneImpl<[CompileTime] T>()
34    {
35        // This compile-time variable will receive the expression representing the base call.
36        // If we have a public Clone method, we will use it (this is the chaining pattern). Otherwise,
37        // we will call MemberwiseClone (this is the initialization of the pattern).
38        IExpression baseCall;
39
40        if ( meta.Target.Method.IsOverride )
41        {
42            baseCall = (IExpression) meta.Base.Clone();
43        }
44        else
45        {
46            baseCall = (IExpression) meta.This.MemberwiseClone();
47        }
48
49        // Define a local variable of the same type as the target type.
50        var clone = (T) baseCall.Value!;
51
52        // Select cloneable fields.
53        var cloneableFields =
54            meta.Target.Type.FieldsAndProperties.Where(
55                f => f.Attributes.OfAttributeType( typeof(ChildAttribute) ).Any() );
56
57        foreach ( var field in cloneableFields )
58        {
59            // Check if we have a public method 'Clone()' for the type of the field.
60            var fieldType = (INamedType) field.Type;
61
62            field.With( clone ).Value = meta.Cast( fieldType, field.Value?.Clone() );
63        }
64
65        return clone;
66    }
67}

The BuildAspect method is the entry point of the aspect.

You can clearly see two steps in this method. We will comment on them independently.

Implementing the interface

The first operation of BuildAspect is to add the ICloneable method to the current type using the ImplementInterface method.

11builder.Advice.ImplementInterface(
12    builder.Target,
13    typeof(ICloneable),
14    OverrideStrategy.Ignore );
15

If the type already implements the ICloneable method, we don't need to do anything, so we are specifying Ignore as the OverrideStrategy. The ImplementInterface method requires the aspect type to include all interface members and to annotate them with the [InterfaceMember] custom attribute.

Our interface implementation calls the public Clone method we will introduce in the type.

29[InterfaceMember( IsExplicit = true )]
30private object Clone() => meta.This.Clone();
31

For details, see Implementing interfaces.

Note that the code uses the expression meta.This, a compile-time expression that returns a dynamic value. Thanks to its dynamic nature, you can write any run-time expression on its right side. This code is not verified until all aspects have been executed, so you can call a method that does not exist yet. For details regarding these techniques, see Generating run-time code

Adding the public method

The second operation of BuildAspect is to introduce a method named Clone by invoking IntroduceMethod.

19builder.Advice.IntroduceMethod(
20    builder.Target,
21    nameof(this.CloneImpl),
22    whenExists: OverrideStrategy.Override,
23    args: new { T = builder.Target },
24    buildMethod: m => m.Name = "Clone" );
25

We set the OverrideStrategy to Override, indicating that the method should be overridden if it already exists in the type. The invocation of IntroduceMethod is more complex than usual for two reasons:

  1. The template method cannot be named Clone because it would conflict with the other Clone method of this aspect, the template for the ICloneable.Clone method. Therefore, we name the template method CloneImpl and rename the introduced method using the delegate passed to the buildMethod parameter. Hence, the code buildMethod: m => m.Name = "Clone".

  2. The CloneImpl template, as we will see below, has a compile-time generic parameter T, where T represents the current type. We need to pass the value of the T parameter in our invocation to the IntroduceMethod method. We pass an anonymous type to the args parameter, with the property T set to its desired value: args: new { T = builder.Target }.

For details, see Introducing members.

Let's now examine the CloneImpl template:

32[Template]
33public virtual T CloneImpl<[CompileTime] T>()
34{
35    // This compile-time variable will receive the expression representing the base call.
36    // If we have a public Clone method, we will use it (this is the chaining pattern). Otherwise,
37    // we will call MemberwiseClone (this is the initialization of the pattern).
38    IExpression baseCall;
39
40    if ( meta.Target.Method.IsOverride )
41    {
42        baseCall = (IExpression) meta.Base.Clone();
43    }
44    else
45    {
46        baseCall = (IExpression) meta.This.MemberwiseClone();
47    }
48
49    // Define a local variable of the same type as the target type.
50    var clone = (T) baseCall.Value!;
51
52    // Select cloneable fields.
53    var cloneableFields =
54        meta.Target.Type.FieldsAndProperties.Where(
55            f => f.Attributes.OfAttributeType( typeof(ChildAttribute) ).Any() );
56
57    foreach ( var field in cloneableFields )
58    {
59        // Check if we have a public method 'Clone()' for the type of the field.
60        var fieldType = (INamedType) field.Type;
61
62        field.With( clone ).Value = meta.Cast( fieldType, field.Value?.Clone() );
63    }
64
65    return clone;
66}

The first half of the method generates the base call with two possibilities:

  • When the method is an override: var clone = (T) base.Clone();
  • Otherwise, when the base type is not deeply clonable: var clone = (T) this.MemberwiseClone();

MemberwiseClone is a standard method of the object class. It returns a shallow copy of the current object. Using the MemberwiseClone has many benefits:

  • It is faster than setting individual fields or properties in C#.
  • It works even when the base type is unaware of the Clone pattern.

meta.Base works similarly to the xref: Metalama.Framework.Aspects.meta.This?text=meta.This> we already used before. It returns a dynamic value, and anything you write on its right side becomes a run-time expression, i.e., C# code injected by the template. To convert this code into a compile-time IExpression object, we cast the dynamic expression into IExpression.

The second part of the CloneImpl template selects all fields and properties annotated with the [Child] attribute and generates code according to the pattern clone.Foo = (FooType?) this.Foo?.Clone(). Fields or properties are represented as compile-time objects by the IFieldOrProperty interface. The Value property operates the same kind of magic as meta.This or meta.Base above, i.e., a dynamic property that can be used in run-time code. By default, field.Value generates a reference to the field for the current instance (i.e. this.Foo). To get the field for a different instance ( e.g. clone.Foo), you must use With.

Summary

In this article, we have created a Cloneable aspect that performs deep cloning of an object by recursively calling the Clone method on child properties. However, we did not validate that the child objects actually have a Clone method or that child properties are not read-only. We will address this problem the following step.