Open sandboxFocus

Interface IAspectBuilder

Provides the context and operations for building an aspect in the BuildAspect(IAspectBuilder<T>) method. This is the primary API for implementing aspect behavior at compile time.

Namespace: Metalama.Framework.Aspects
Assembly: Metalama.Framework.dll
Syntax
[CompileTime]
public interface IAspectBuilder : IAdviser
Remarks

The IAspectBuilder is the central API that aspects use to transform code, report diagnostics, and manage aspect composition. It is passed to your aspect's BuildAspect(IAspectBuilder<T>) method when the aspect is being applied to a target declaration.

Key capabilities:

  • Advising (code transformation): Use extension methods from AdviserExtensions such as builder.Override(), builder.IntroduceMethod(), builder.ImplementInterface(), etc.
  • Child aspects: Apply additional aspects to related declarations via Outbound
  • Diagnostics: Report errors, warnings, or suppress diagnostics through Diagnostics
  • Validation: Register validators that run after all aspects have been applied
  • Context access: Read the Target declaration, Project, and AspectInstance metadata
  • Aspect control: Skip aspect application via SkipAspect() or store instance-specific data in AspectState

Target vs AdvisedTarget: The Target property provides the declaration in its original state (before the current aspect was applied), while AdvisedTarget includes modifications made by the current aspect.

This is the non-generic base interface. Use the strongly-typed IAspectBuilder<TAspectTarget> variant in your BuildAspect(IAspectBuilder<T>) implementation for type-safe access to the target declaration.

Properties

Name Description
Advice

Gets an object that allows to create advice, e.g. overriding members, introducing members, or implementing new interfaces.

AspectInstance

Gets the current IAspectInstance, which provides metadata about this aspect instance.

AspectState

Gets or sets an arbitrary object that is then exposed on the AspectState property of the IAspectInstance interface. While a single instance of an aspect class can be used for several target declarations, the AspectState is specific to the target declaration. If the aspect is inherited, the AspectState must be compile-time-serializable (ICompileTimeSerializable or default serializable classes).

CancellationToken

Gets the cancellation token for the current operation. Use this to detect when the build process has been cancelled and abort long-running operations gracefully.

IsAspectSkipped

Gets a value indicating whether the SkipAspect() method was called.

Layer

Gets the name of the layer being built, or null if this is the default (initial) layer. When an aspect has several layers, the BuildAspect(IAspectBuilder<T>) method is called several times. To register aspect layers, add the LayersAttribute custom attribute to the aspect class.

Project

Gets the current IProject, which represents the csproj file and allows to share project-local data.

Tags

Gets or sets the tags passed to all advice added by the current BuildAspect(IAspectBuilder<T>) method. These tags can be consumed from the Tags property in template code.

Methods

Name Description
SkipAspect()

Cancels the application of this aspect to the target declaration.

WithTarget<T>(T)
With<T>(T)

Returns a copy of the current IAspectBuilder, for use in the current execution context, but for a different Target declaration.

Extension Methods

See Also