| Severity | Error |
| Reported by | Metalama.Framework |
| Message | The aspect '<AspectType>' cannot implement the interface '<InterfaceType>' in the type '<TargetType>' because the strategy 'whenExists=<Strategy>' is not supported for interfaces. Use Fail, Ignore, or Override. To handle conflicts with individual members, use the WhenExists property of the [InterfaceMember] attribute. |
Cause
The whenExists parameter of ImplementInterface determines what Metalama does when the target type, or one of its base types, already implements the interface. It accepts only the following values of OverrideStrategy:
- Fail (the default): Metalama reports an error.
- Ignore: the advice is ignored.
- Override: the members of the interface are implemented again, overriding existing members when necessary.
Metalama reports LAMA0516 for any other value, which in practice means OverrideStrategy.New: an interface implementation can't be redefined with the new modifier.
In the following example, the Describable aspect passes OverrideStrategy.New to ImplementInterface.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0516.Error;
5
6public interface IDescribable
7{
8 string Describe();
9}
10
11public class DescribableAttribute : TypeAspect
12{
13 public override void BuildAspect( IAspectBuilder<INamedType> builder )
14 {
15 // Error: OverrideStrategy.New is not supported for interfaces.
16 builder.ImplementInterface( typeof(IDescribable), OverrideStrategy.New );
17 }
18
19 [InterfaceMember]
20 public string Describe() => meta.Target.Type.Name;
21}
22
1namespace Doc.LAMA0516.Error;
2
3[Describable]
Error LAMA0516: The aspect 'Describable' cannot implement the interface 'IDescribable' in the type 'Order' because the strategy 'whenExists=New' is not supported for interfaces. Use Fail, Ignore, or Override. To handle conflicts with individual members, use the WhenExists property of the [InterfaceMember] attribute.
4internal partial class Order
5{
6 public int Id { get; set; }
7}
8
Resolution
Pass OverrideStrategy.Fail, OverrideStrategy.Ignore, or OverrideStrategy.Override to ImplementInterface. Ignore is the most common choice: it skips the interface when the type already implements it.
To control what happens when the target type already contains a member with the same signature as an interface member, use the WhenExists property of the [InterfaceMember] attribute instead.
In the following example, the aspect uses OverrideStrategy.Ignore.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0516.Fixed;
5
6public interface IDescribable
7{
8 string Describe();
9}
10
11public class DescribableAttribute : TypeAspect
12{
13 public override void BuildAspect( IAspectBuilder<INamedType> builder )
14 {
15 // Fixed: OverrideStrategy.Ignore is supported for interfaces.
16 builder.ImplementInterface( typeof(IDescribable), OverrideStrategy.Ignore );
17 }
18
19 [InterfaceMember]
20 public string Describe() => meta.Target.Type.Name;
21}
22
1namespace Doc.LAMA0516.Fixed;
2
3[Describable]
4internal partial class Order
5{
6 public int Id { get; set; }
7}
8
1namespace Doc.LAMA0516.Fixed;
2
3[Describable]
4internal partial class Order : IDescribable
5{
6 public int Id { get; set; }
7
8 public string Describe()
9 {
10 return "Order";
11 }
12}
13