Open sandboxFocusImprove this doc

LAMA0037: The aspect can't be applied to the declaration

Severity Error
Reported by Metalama.Framework
Message The aspect '<AspectName>' cannot be applied to the <DeclarationKind> '<Target>' because <Reason>.

Cause

Every aspect can declare eligibility conditions in its BuildEligibility method. These conditions describe the declarations to which the aspect can be applied. For instance, an aspect can require the target method to be non-static or to have a non-void return type.

Metalama reports LAMA0037 when you apply an aspect to a declaration, typically with a custom attribute, but the declaration doesn't satisfy one of these conditions. The last part of the message explains which condition failed.

In the following example, the Log aspect requires an instance method, but it's applied to a static method.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using Metalama.Framework.Eligibility;
4using System;
5
6namespace Doc.LAMA0037.Error;
7
8public class LogAttribute : OverrideMethodAspect
9{
10    public override void BuildEligibility( IEligibilityBuilder<IMethod> builder )
11    {
12        base.BuildEligibility( builder );
13
14        // The aspect requires an instance method.
15        builder.MustNotBeStatic();
16    }
17
18    public override dynamic? OverrideMethod()
19    {
20        Console.WriteLine( $"Executing {meta.Target.Method}." );
21
22        return meta.Proceed();
23    }
24}
25
1namespace Doc.LAMA0037.Error;
2
3internal class Calculator
4{
5    // Error: the [Log] aspect is not eligible on static methods.
    Error LAMA0037: The aspect 'Log' cannot be applied to the method 'Calculator.Add(int, int)' because 'Calculator.Add(int, int)' must not be static.

6    [Log]
7    public static int Add( int a, int b ) => a + b;
8}
9

Resolution

Depending on your intent, do one of the following:

  • Remove the aspect from the declaration.
  • Change the declaration so that it satisfies the condition given in the message. In the following example, the method is made non-static.
  • If you're the author of the aspect and the condition is too strict, relax the condition in the aspect's BuildEligibility method.

When you add aspects with a fabric, use the Where method to select only eligible declarations instead of adding aspects to all declarations.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using Metalama.Framework.Eligibility;
4using System;
5
6namespace Doc.LAMA0037.Fixed;
7
8public class LogAttribute : OverrideMethodAspect
9{
10    public override void BuildEligibility( IEligibilityBuilder<IMethod> builder )
11    {
12        base.BuildEligibility( builder );
13
14        // The aspect requires an instance method.
15        builder.MustNotBeStatic();
16    }
17
18    public override dynamic? OverrideMethod()
19    {
20        Console.WriteLine( $"Executing {meta.Target.Method}." );
21
22        return meta.Proceed();
23    }
24}
25
Source Code
1namespace Doc.LAMA0037.Fixed;
2


3internal class Calculator
4{
5    // Fixed: the method is no longer static, so the aspect is eligible.
6    [Log]
7    public int Add( int a, int b ) => a + b;
8}
9
Transformed Code
1using System;
2
3namespace Doc.LAMA0037.Fixed;
4
5internal class Calculator
6{
7    // Fixed: the method is no longer static, so the aspect is eligible.
8    [Log]
9    public int Add(int a, int b)
10    {
11        Console.WriteLine("Executing Calculator.Add(int, int).");
12        return a + b;
13    }
14}
15

See also