Open sandboxFocusImprove this doc

LAMA0517: The template of an interface property is missing an accessor

Severity Error
Reported by Metalama.Framework
Message The aspect '<AspectType>' cannot implement the interface property '<InterfaceProperty>' in the type '<TargetType>' because the template '<TemplateMember>' does not have the '<AccessorKind>' accessor required by the interface property.

Cause

When an aspect implements an interface with ImplementInterface, each property of the interface is implemented by the property of the aspect that has the same name and type and is marked with [InterfaceMember]. This template property must have every accessor that the interface property declares.

Metalama reports LAMA0517 when the template property:

  • has no get accessor, but the interface property has one;
  • has no set or init accessor, but the interface property has one;
  • has an init accessor, but the interface property has a set accessor, or the reverse.

The rule applies to implicit and explicit implementations alike. When the template is implicit, Metalama reports LAMA0517 for a missing accessor before it checks the accessibility of the accessors, so you don't get LAMA0519 for the same template.

In the message, <AccessorKind> is the accessor that the template is missing.

In the following example, the ITrackable.IsChanged property has a getter and a setter, but the template has only a getter.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0517.Error;
5
6public interface ITrackable
7{
8    bool IsChanged { get; set; }
9}
10
11public class TrackableAttribute : TypeAspect
12{
13    public override void BuildAspect( IAspectBuilder<INamedType> builder )
14    {
15        builder.ImplementInterface( typeof(ITrackable) );
16    }
17
18    // Error: the interface property has a setter, but the template does not.
19    [InterfaceMember]
20    public bool IsChanged { get => false; }
21}
22
1namespace Doc.LAMA0517.Error;
2
3[Trackable]
Error LAMA0517: The aspect 'Trackable' cannot implement the interface property 'ITrackable.IsChanged' in the type 'Order' because the template 'TrackableAttribute.IsChanged' does not have the 'set' accessor required by the interface property.

4internal partial class Order
5{
6    public int Id { get; set; }
7}
8

Resolution

Add the missing accessor to the template property, with the same kind (set or init) as in the interface property. If the property only needs to store a value, make the template an automatic property with the accessors of the interface property, as in the following example.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0517.Fixed;
5
6public interface ITrackable
7{
8    bool IsChanged { get; set; }
9}
10
11public class TrackableAttribute : TypeAspect
12{
13    public override void BuildAspect( IAspectBuilder<INamedType> builder )
14    {
15        builder.ImplementInterface( typeof(ITrackable) );
16    }
17
18    // Fixed: the template has both accessors of the interface property.
19    [InterfaceMember]
20    public bool IsChanged { get; set; }
21}
22
Source Code
1namespace Doc.LAMA0517.Fixed;
2

3[Trackable]
4internal partial class Order
5{
6    public int Id { get; set; }
7}
8
Transformed Code
1namespace Doc.LAMA0517.Fixed;
2
3[Trackable]
4internal partial class Order : ITrackable
5{
6    public int Id { get; set; }
7
8
9    public bool IsChanged { get; set; }
10}
11

See also