MetalamaCommented examplesCloneStep 1.​ Getting started
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: ICloneable
5{
6    public Player Player { get; set; }
7
8    [Child]
9    public GameSettings Settings { get; set; }
10
11    public virtual Game Clone()
12    {
13        var clone = (Game)this.MemberwiseClone();
14        clone.Settings = this.Settings.Clone();
15        return clone;
16    }
17
18    object ICloneable.Clone()
19    {
20        return Clone();
21    }
22}
23
Source Code


1[Cloneable]
2internal class GameSettings
3{
4    public int Level { get; set; }
5    public string World { get; set; }





6}







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

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

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

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 <xref:System.ICloneable> method to the current type using the ImplementInterface method.

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

If the type already implements the <xref:System.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.

23    [InterfaceMember( IsExplicit = true )]
24    private object Clone() => meta.This.Clone();

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.

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

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 <xref:System.ICloneable.Clone*?text=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:

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

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();

<xref:System.Object.MemberwiseClone*> is a standard method of the <xref:System.Object> class. It returns a shallow copy of the current object. Using the <xref:System.Object.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.