Metalama 1.0 / / Metalama Documentation / Aspect API Documentation / Metalama.​Framework.​Validation

Namespace Metalama.Framework.Validation

This is namespace allows you to validate your code, the code that uses your aspects, or the code that references the code that uses your aspects.


Aspect can register validators from their implementation of IAspect.BuildAspect, and fabrics from their implementation of AmendType, AmendNamespace or AmendProject.

From these methods, call the amender.With method exposed on the builder or amender parameter, then call Validate or ValidateReferences. These methods allow you to register a delegate. This delegate is then called and receive a context object of type DeclarationValidationContext or ReferenceValidationContext. The delegate can then analyze the code or reference, and report diagnostics.

The ReportDiagnostic, SuppressDiagnostic and SuggestCodeFix methods are provided for convenience, and use Validate.

To validate whether an aspect is eligible for a declaration (which involves validating the compilation before the aspect has been applied), implement the BuildEligibility aspect method.

Validated revision of the code model

Since aspects can modify the code model, it can be useful to be aware of which revision of the code model is validated.

  • The ValidateReferences always validates the source code. References introduced by aspects cannot be validated.

  • By default, fabrics validate the source code. By calling AfterAllAspects(), fabrics can validate the code model after all aspects have been applied.

  • By default, aspects validate the code as it is before they are executed (see Aspect composition). Call AfterAllAspects() to validate the code after all aspects have been applied, or BeforeAnyAspect() to validate the source code.

Class Diagram

classDiagram class ValidatorDelegate_Of_DeclarationValidationContext{ <<delegate>> Invoke(context) } class ValidatorDelegate_Of_ReferenceValidationContext{ <<delegate>> Invoke(context) } class DeclarationValidationContext { AspectState Declaration Diagnostics } class ReferenceValidationContext { AspectState DiagnosticLocation ReferencedDeclaration ReferencedType ReferenceKinds Syntax } class IValidatorReceiver { ValidateReferences() Validate() ReportDiagnostic() SuppressDiagnostic() SuggestCodeFix() } IValidatorReceiver --> ValidatorDelegate_Of_DeclarationValidationContext : registers IValidatorReceiver --> ValidatorDelegate_Of_ReferenceValidationContext : registers ValidatorDelegate_Of_DeclarationValidationContext --> DeclarationValidationContext : receives ValidatorDelegate_Of_ReferenceValidationContext --> ReferenceValidationContext : receives class IValidatorReceiverSelector { With() AfterAllAspects() BeforeAnyAspect() } IValidatorReceiverSelector --> IValidatorReceiver : creates IValidatorReceiverSelector --> IValidatorReceiverSelector IAspectBuilder --|> IValidatorReceiverSelector : derives from IAmender --|> IValidatorReceiverSelector : derives from IAspect --> IAspectBuilder : receives Fabric --> IAmender : receives

Namespace Members



The context object passed to the single parameter of validators added using Validate(ValidatorDelegate<DeclarationValidationContext>).


A custom attribute that means that the interface cannot be implemented by another assembly than the one that declared it, except if the referencing assembly sees the internals of the declaring assembly. (The enforcement of this attribute is not implemented.)


Enumerates all kinds of references.


The context object passed to the single parameter of validators added using ValidateReferences(ValidatorDelegate<ReferenceValidationContext>, ReferenceKinds).


A delegate for validator methods. The TContext type argument can be either DeclarationValidationContext or ReferenceValidationContext.




An interface that allows aspects and fabrics to register validators for the initial or final compilation version.