Open sandboxFocusImprove this doc

Implementing interfaces

Certain aspects necessitate modifying the target type to implement a new interface. This can only be achieved by using the programmatic advising API.

Step 1. Call AdviserExtensions.ImplementInterface

Within your implementation of the BuildAspect method, invoke the ImplementInterface method.

You might need to pass a value to the OverrideStrategy parameter to cope with the situation where the target type, or any of its ancestors, already implements the interface. The most common behavior is OverrideStrategy.Ignore, but the default value is OverrideStrategy.Fail, consistent with other advice kinds.

Note

Unlike in PostSharp, it is not necessary in Metalama for the aspect class to implement the introduced interface.

Step 2, Option A. Add interface members to the aspect class, declaratively

The next step is to ensure that the aspect class generates all interface members. We can do this declaratively or programmatically and add implicit or explicit implementations.

Note

The ImplementInterface method does not verify if the aspect generates all required members. If your aspect fails to introduce a member, the C# compiler will report errors.

Let's start with the declarative approach.

Implement all interface members in the aspect and annotate them with the [InterfaceMember] custom attribute. This attribute instructs Metalama to introduce the member to the target class but only if the ImplementInterface succeeds. If the advice is ignored because the type already implements the interface and OverrideStrategy.Ignore has been used, the member will not be introduced to the target type.

By default, an implicit (public) implementation is created. You can use the IsExplicit property to specify that an explicit implementation must be created instead of a public method.

Note

Using the [Introduce] also works but is not recommended in this case because this approach ignores the result of the ImplementInterface method.

Example: IDisposable

In the subsequent example, the aspect introduces the IDisposable interface. The implementation of the Dispose method disposes of all fields or properties that implement the IDisposable interface.

1using Metalama.Framework.Advising;
2using Metalama.Framework.Aspects;
3using Metalama.Framework.Code;
4using System;
5using System.Linq;
6
7namespace Doc.Disposable;
8
9internal class DisposableAttribute : TypeAspect
10{
11    public override void BuildAspect( IAspectBuilder<INamedType> builder )
12    {
13        builder.ImplementInterface(
14            typeof(IDisposable),
15            whenExists: OverrideStrategy.Ignore );
16    }
17
18    // Introduces a the `Dispose(bool)` protected method, which is NOT an interface member.
19    [Introduce( Name = "Dispose", IsVirtual = true, WhenExists = OverrideStrategy.Override )]
20    protected void DisposeImpl( bool disposing )
21    {
22        // Call the base method, if any.
23        meta.Proceed();
24
25        var disposableFields = meta.Target.Type.FieldsAndProperties
26            .Where( x => x.Type.Is( typeof(IDisposable) ) && x.IsAutoPropertyOrField == true );
27
28        // Disposes the current field or property.
29        foreach ( var field in disposableFields )
30        {
31            field.Value?.Dispose();
32        }
33    }
34
35    // Implementation of IDisposable.Dispose.
36    [InterfaceMember]
37    public void Dispose()
38    {
39        meta.This.Dispose( true );
40    }
41}
Source Code
1using System.IO;
2using System.Threading;

3
4#pragma warning disable CA1001 // Types that own disposable fields should be disposable
5
6namespace Doc.Disposable;
7
8[Disposable]
9internal class Foo
10{
11    private CancellationTokenSource _cancellationTokenSource = new();
12}
13




14[Disposable]
15internal class Bar : Foo






16{
17    private MemoryStream _stream = new();
18}
Transformed Code
1using System;
2using System.IO;
3using System.Threading;
4
5#pragma warning disable CA1001 // Types that own disposable fields should be disposable
6
7namespace Doc.Disposable;
8
9[Disposable]
10internal class Foo : IDisposable
11{
12    private CancellationTokenSource _cancellationTokenSource = new();
13
14    public void Dispose()
15    {
16        this.Dispose(true);
17    }
18
19    protected virtual void Dispose(bool disposing)
20    {
21        _cancellationTokenSource.Dispose();
22    }
23}
24
25[Disposable]
26internal class Bar : Foo
27{
28    private MemoryStream _stream = new();
29
30    protected override void Dispose(bool disposing)
31    {
32        base.Dispose(disposing);
33        _stream.Dispose();
34    }
35}

Example: Deep cloning

1using Metalama.Framework.Advising;
2using Metalama.Framework.Aspects;
3using Metalama.Framework.Code;
4using System;
5using System.Linq;
6
7namespace Doc.DeepClone;
8
9[Inheritable]
10public class DeepCloneAttribute : TypeAspect
11{
12    public override void BuildAspect( IAspectBuilder<INamedType> builder )
13    {
14        builder.IntroduceMethod(
15            nameof(this.CloneImpl),
16            whenExists: OverrideStrategy.Override,
17            buildMethod: t =>
18            {
19                t.Name = "Clone";
20                t.ReturnType = builder.Target;
21            } );
22
23        builder.ImplementInterface(
24            typeof(ICloneable),
25            whenExists: OverrideStrategy.Ignore );
26    }
27
28    [Template( IsVirtual = true )]
29    public virtual dynamic CloneImpl()
30    {
31        // This compile-time variable will receive the expression representing the base call.
32        // If we have a public Clone method, we will use it (this is the chaining pattern). Otherwise,
33        // we will call MemberwiseClone (this is the initialization of the pattern).
34        IExpression baseCall;
35
36        if ( meta.Target.Method.IsOverride )
37        {
38            baseCall = meta.Base.Clone();
39        }
40        else
41        {
42            baseCall = meta.Base.MemberwiseClone();
43        }
44
45        // Define a local variable of the same type as the target type.
46        var clone = meta.Cast( meta.Target.Type, baseCall );
47
48        // Select clonable fields.
49        var clonableFields =
50            meta.Target.Type.FieldsAndProperties.Where(
51                f => f.IsAutoPropertyOrField == true &&
52                     ((f.Type.Is( typeof(ICloneable) ) && f.Type.SpecialType != SpecialType.String)
53                      ||
54                      (f.Type is INamedType { BelongsToCurrentProject: true } fieldNamedType &&
55                       fieldNamedType.Enhancements().HasAspect<DeepCloneAttribute>())) );
56
57        foreach ( var field in clonableFields )
58        {
59            // Check if we have a public method 'Clone()' for the type of the field.
60            var fieldType = (INamedType) field.Type;
61            var cloneMethod = fieldType.Methods.OfExactSignature( "Clone", Array.Empty<IType>() );
62
63            IExpression callClone;
64
65            if ( cloneMethod is { Accessibility: Accessibility.Public } ||
66                 fieldType.Enhancements().HasAspect<DeepCloneAttribute>() )
67            {
68                // If yes, call the method without a cast.
69                callClone = field.Value?.Clone()!;
70            }
71            else
72            {
73                // If no, explicitly cast to the interface.
74                callClone = (IExpression) ((ICloneable?) field.Value)?.Clone()!;
75            }
76
77            if ( cloneMethod == null || !cloneMethod.ReturnType.ToNullableType().Is( fieldType ) )
78            {
79                // If necessary, cast the return value of Clone to the field type.
80                callClone = (IExpression) meta.Cast( fieldType, callClone.Value );
81            }
82
83            // Finally, set the field value.
84            field.With( (IExpression) clone ).Value = callClone.Value;
85        }
86
87        return clone;
88    }
89
90    [InterfaceMember( IsExplicit = true )]
91    private object Clone()
92    {
93        return meta.This.Clone();
94    }
95}
Source Code
1using System;
2
3namespace Doc.DeepClone;
4
5internal class ManuallyCloneable : ICloneable
6{
7    public object Clone()
8    {
9        return new ManuallyCloneable();
10    }
11}
12
13[DeepClone]
14internal class AutomaticallyCloneable
15{
16    private int _a;
17    private ManuallyCloneable? _b;
18    private AutomaticallyCloneable? _c;
19}
20







21internal class DerivedCloneable : AutomaticallyCloneable
22{






23    private string? _d;
24}
Transformed Code
1using System;
2
3namespace Doc.DeepClone;
4
5internal class ManuallyCloneable : ICloneable
6{
7    public object Clone()
8    {
9        return new ManuallyCloneable();
10    }
11}
12
13[DeepClone]
14internal class AutomaticallyCloneable : ICloneable
15{
16    private int _a;
17    private ManuallyCloneable? _b;
18    private AutomaticallyCloneable? _c;
19
20    public virtual AutomaticallyCloneable Clone()
21    {
22        var clone = (AutomaticallyCloneable)MemberwiseClone();
23        clone._b = (ManuallyCloneable?)_b?.Clone()!;
24        clone._c = _c?.Clone()!;
25        return clone;
26    }
27
28    object ICloneable.Clone()
29    {
30        return Clone();
31    }
32}
33
34internal class DerivedCloneable : AutomaticallyCloneable
35{
36    private string? _d;
37
38    public override DerivedCloneable Clone()
39    {
40        var clone = (DerivedCloneable)base.Clone();
41        return clone;
42    }
43}

Step 2, Option B. Add interface members to the aspect class, programmatically

This approach can be used instead or in complement to the declarative one.

It is useful in the following situations:

  • when the introduced interface is unknown to the aspect's author, e.g., when it can be dynamically specified by the aspect's user;
  • when introducing a generic interface thanks to the ability to use generic templates (see Template parameters and type parameters).

To programmatically add interface members, use one of the Introduce methods of the AdviserExtensions class, as explained in Introducing members. Make sure that these members are public.

If instead of adding public members you need to add explicit implementations, use the ExplicitMembers property of the IImplementInterfaceAdviceResult returned by the ImplementInterface method, and call any of its Introduce methods.

Referencing interface members in other templates

When introducing an interface member to the type, you often want to access it from templates. Unless the member is an explicit implementation, you have two options:

Option 1. Access the aspect template member

this.Dispose();

Option 2. Use meta.This and write dynamic code

meta.This.Dispose();

Option 3. Use invokers

If the members have been added programmatically, you can use invoker APIs like IMethod.Invoke for methods or Value for properties.

Accessing explicit implementations

The following strategies can be employed to access explicit implementations:

  • Cast the instance to the interface and access the member:

    ((IDisposable)meta.This).Dispose();
    
  • Introduce a private method with the concrete method implementation, and call this private member both from the interface member and the templates.