| Severity | Error |
| Reported by | Metalama.Framework |
| Message | Accessor '<Accessor>' cannot have the '<Attribute>' attribute. Add the attribute to the containing <MemberKind> instead. |
Cause
Template attributes such as TemplateAttribute and advice attributes such as IntroduceAttribute apply to a whole property or event, including its accessors.
Metalama reports LAMA0262 when one of these attributes is applied to an accessor, such as get, set, add, or remove. The message gives the accessor, the attribute, and the kind of the containing member (property or event).
In the following example, the [Introduce] attribute is applied to the getter of the SchemaVersion property.
1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0262.Error;
4
5public class VersionedAttribute : TypeAspect
6{
7 public int SchemaVersion
8 {
9 // Error: the [Introduce] attribute cannot be applied to an accessor.
10 [Introduce]
Error LAMA0262: Accessor 'VersionedAttribute.SchemaVersion.get' cannot have the 'IntroduceAttribute' attribute. Add the attribute to the containing property instead.
11 get => 1;
12 }
13}
14
1namespace Doc.LAMA0262.Error;
2
3[Versioned]
4internal partial class Invoice
5{
6 public decimal Amount { get; set; }
7}
8
Resolution
Move the attribute from the accessor to the containing property or event, as in the following example.
If the property or event already has a template or advice attribute, remove the one on the accessor. Otherwise, Metalama also reports LAMA0261.
1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0262.Fixed;
4
5public class VersionedAttribute : TypeAspect
6{
7 // Fixed: the [Introduce] attribute is applied to the property.
8 [Introduce]
9 public int SchemaVersion
10 {
11 get => 1;
12 }
13}
14
1namespace Doc.LAMA0262.Fixed;
2
3[Versioned]
4internal partial class Invoice
5{
6 public decimal Amount { get; set; }
7}
8
1namespace Doc.LAMA0262.Fixed;
2
3[Versioned]
4internal partial class Invoice
5{
6 public decimal Amount { get; set; }
7
8 public int SchemaVersion
9 {
10 get
11 {
12 return 1;
13 }
14 }
15}
16