| Severity | Error |
| Reported by | Metalama.Framework |
| Message | The aspect '<AspectType>' cannot introduce a parameter into '<Constructor>' because it is a static constructor. |
Cause
The IntroduceParameter advice appends a parameter to a constructor, typically to inject a dependency. A static constructor can't have parameters, because the runtime calls it implicitly and no code can pass arguments to it.
LAMA0520 is defined for an aspect that tries to introduce a parameter into a static constructor. However, Metalama checks the eligibility of the target as soon as the aspect calls IntroduceParameter, and this check rejects static constructors by throwing an InvalidOperationException. If the aspect doesn't catch the exception, you get LAMA0041 instead of LAMA0520, with a message saying that the constructor must not be static.
Because the eligibility check always runs first, this diagnostic can't be shown in an example.
Resolution
Introduce the parameter into instance constructors only:
- When you select the target constructors, skip the static constructor, for example by iterating over the Constructors collection of the type, which doesn't include it, or by filtering on IsStatic.
- To check whether a constructor can receive a parameter before adding the advice, call the IsAdviceEligible extension method with AdviceKind.IntroduceParameter.
If you need to initialize static state, use a static field or a type initializer instead of a constructor parameter.