| Severity | Error |
| Reported by | Metalama.Framework |
| Message | The aspect '<AspectType>' cannot introduce the abstract member '<Member>' with the override strategy '<OverrideStrategy>' because an introduced abstract member cannot override or hide an existing member. Use the override strategy 'Fail' or 'Ignore'. |
Cause
When an aspect introduces a member into an abstract class, the introduced member is abstract if its template has no body (an extern template) or if the template sets the IsAbstract property.
The whenExists argument of methods like IntroduceMethod sets the OverrideStrategy, which decides what happens when the target type or one of its base types already has a member with the same signature. The Override strategy replaces the implementation of the existing member, and the New strategy hides it. Neither strategy makes sense for an abstract member, which has no implementation. Metalama reports LAMA0533 when it introduces an abstract member with either strategy, even if no member with the same signature exists.
In the following example, the Validatable aspect introduces an abstract Validate method with OverrideStrategy.Override.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0533.Error;
5
6public class ValidatableAttribute : TypeAspect
7{
8 public override void BuildAspect( IAspectBuilder<INamedType> builder )
9 {
10 // Error: an abstract method can't be introduced with OverrideStrategy.Override.
11 builder.IntroduceMethod( nameof(this.Validate), whenExists: OverrideStrategy.Override );
12 }
13
14 // A template without a body introduces an abstract method into an abstract class.
15#pragma warning disable CS0626
16 [Template]
17 public extern void Validate();
18#pragma warning restore CS0626
19}
20
1namespace Doc.LAMA0533.Error;
2
3// The [Validatable] aspect introduces an abstract Validate method into this class.
4[Validatable]
Error LAMA0533: The aspect 'Validatable' cannot introduce the abstract member 'Entity.Validate()' with the override strategy 'Override' because an introduced abstract member cannot override or hide an existing member. Use the override strategy 'Fail' or 'Ignore'.
5internal abstract class Entity
6{
7 public int Id { get; set; }
8}
9
Resolution
Do one of the following:
- Use the Fail strategy, which is the default, so you can omit the
whenExistsargument, or the Ignore strategy, which keeps the existing member. The following example usesIgnore. - If the introduced member must override an existing member, give the template a body so that the introduced member isn't abstract.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0533.Fixed;
5
6public class ValidatableAttribute : TypeAspect
7{
8 public override void BuildAspect( IAspectBuilder<INamedType> builder )
9 {
10 // Fixed: OverrideStrategy.Ignore keeps an existing Validate method instead of overriding it.
11 builder.IntroduceMethod( nameof(this.Validate), whenExists: OverrideStrategy.Ignore );
12 }
13
14 // A template without a body introduces an abstract method into an abstract class.
15#pragma warning disable CS0626
16 [Template]
17 public extern void Validate();
18#pragma warning restore CS0626
19}
20
1namespace Doc.LAMA0533.Fixed;
2
3// The [Validatable] aspect introduces an abstract Validate method into this class.
4[Validatable]
5internal abstract class Entity
6{
7 public int Id { get; set; }
8}
9
1namespace Doc.LAMA0533.Fixed;
2
3// The [Validatable] aspect introduces an abstract Validate method into this class.
4[Validatable]
5internal abstract class Entity
6{
7 public int Id { get; set; }
8
9 public abstract void Validate();
10}
11