Open sandboxFocusImprove this doc

LAMA0541: An extension block can't be introduced into another extension block

Severity Error
Reported by Metalama.Framework
Message The aspect '<AspectType>' cannot introduce an extension block for type '<ReceiverType>' into '<TargetType>' because the target is an extension block. Extension blocks can only be introduced into static classes.

Cause

The IntroduceExtensionBlock method returns an advice result that is itself an adviser, so you can call introduction methods such as IntroduceMethod on it to add members to the new extension block. C# doesn't allow extension blocks to be nested, so calling IntroduceExtensionBlock on this result isn't valid. In the message, <ReceiverType> is the receiver type of the new extension block and <TargetType> is the existing extension block.

LAMA0541 is a safeguard reported when the advice is implemented. However, Metalama checks the target when the aspect calls IntroduceExtensionBlock, and throws an exception because an extension block isn't a top-level static class. In current versions, you therefore see LAMA0041 instead of LAMA0541, with a message such as Cannot add an IntroduceExtensionBlock advice to 'StringExtensions.extension(string)' because 'StringExtensions.extension(string)' 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

Call IntroduceExtensionBlock on the adviser of the static class, not on the result of a previous IntroduceExtensionBlock call. To extend several types, introduce one extension block per receiver type into the same static class, and add the members of each block through its own advice result.

See also