| Severity | Error |
| Reported by | Metalama.Framework |
| Message | The aspect '<AspectType>' cannot introduce an extension block for type '<ReceiverType>' into '<TargetType>' because the target type is not a static class. |
Cause
In C#, extension blocks (extension( T receiver ) { ... }) can only be declared in a top-level, non-generic static class. An aspect introduces an extension block with the IntroduceExtensionBlock method, and the target of this advice must therefore be such a class. In the message, <ReceiverType> is the receiver type, that is, the type being extended, and <TargetType> is the type into which the aspect tries to introduce the extension block.
LAMA0540 is a safeguard reported when the advice is implemented. However, Metalama checks the target when the aspect calls IntroduceExtensionBlock, and throws an exception if the target isn't a top-level static class. In current versions, you therefore see LAMA0041 instead of LAMA0540, with a message such as Cannot add an IntroduceExtensionBlock advice to 'StringHelper' because 'StringHelper' must be a top-level static class.
Metalama doesn't report this diagnostic in practice, so it can't be shown in an example.
Resolution
Introduce the extension block into a top-level static class:
- If the aspect is applied to a static class, make sure that the target class is marked
staticand isn't nested in another type. - If the aspect is applied to another kind of type, for instance the type being extended, introduce a new static class first with IntroduceClass, setting IsStatic to
truein thebuildTypedelegate, then call IntroduceExtensionBlock on the result. - In the aspect's
BuildAspectmethod, check the target withIsAdviceEligible( AdviceKind.IntroduceExtensionBlock )before adding the advice when the target may not be a static class.