Open sandboxFocus

Method AddAnnotation

AddAnnotation<TDeclaration>(IAdviser<TDeclaration>, IAnnotation<TDeclaration>, bool)

Adds a custom annotation to a declaration. Annotations enable communication between aspects by attaching arbitrary serializable objects to declarations that other aspects can query.

Declaration
public static void AddAnnotation<TDeclaration>(this IAdviser<TDeclaration> adviser, IAnnotation<TDeclaration> annotation, bool export = false) where TDeclaration : class, IDeclaration
Parameters
Type Name Description
IAdviser<TDeclaration> adviser

An adviser for a declaration.

IAnnotation<TDeclaration> annotation

The annotation to attach to the target declaration.

bool export

A value indicating whether the annotation should be exported and made visible to other projects. Unless this parameter is set to true, the annotation will only be visible to the current project.

Type Parameters
Name Description
TDeclaration

The type of declaration.

Remarks

Annotations are useful for:

  • Inter-aspect communication: Pass information from one aspect to another without using aspect inheritance
  • Flagging declarations: Mark declarations for special processing by later aspects
  • Storing computed data: Attach data that other aspects need during code generation

Reading annotations: Use GetAnnotations<TAnnotation>() via Enhancements<T>(T) to retrieve annotations from any declaration.

Visibility: By default, annotations are only visible within the current project. Set export to true to make them visible to projects that reference this one.

Targeting other declarations: Use With<TNewDeclaration>(TNewDeclaration) to add annotations to declarations other than the current target.

Annotations vs AspectState: Use AspectState to share state with child aspects and inheriting aspects via the predecessor chain. Use annotations for peer-to-peer aspect communication.

Design-time limitation: At design time, Metalama performs partial compilations that only include the inheritance closure of modified files. Aspects targeting declarations outside this scope do not execute, so their annotations are unavailable. This does not affect full compile time.

See Also