MetalamaAPI documentationAspect APIMetalama.​Framework.​Validation
Open sandboxFocusImprove this doc

Metalama.Framework.Validation Namespace

This namespace enables validation of your code, the code that utilizes your aspects, or the code that references the code that uses your aspects.

Overview

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

From these methods, invoke 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 subsequently called and receives 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 utilize 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 beneficial to be aware of which revision of the code model is being validated.

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

  • By default, fabrics validate the source code. By invoking 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~DeclarationValidationContext~{
        <<delegate>>
        Invoke(context)
    }

     class ` ValidatorDelegate~ReferenceValidationContext~`{
        <<delegate>>
        Invoke(context)
    }

    class DeclarationValidationContext {
        AspectState
        Declaration
        Diagnostics
    }

    class ReferenceValidationContext {
        AspectState
        DiagnosticLocation
        ReferencedDeclaration
        ReferencedType
        ReferenceKinds
        Syntax
    }


    class IValidatorReceiver {
        ValidateReferences()
        Validate()
        ReportDiagnostic()
        SuppressDiagnostic()
        SuggestCodeFix()
    }

    ValidatorDelegate~DeclarationValidationContext~ <-- IValidatorReceiver : registers
    ` ValidatorDelegate~ReferenceValidationContext~` <-- IValidatorReceiver : registers

DeclarationValidationContext <-- ValidatorDelegate~DeclarationValidationContext~  : receives
    ReferenceValidationContext <-- ` ValidatorDelegate~ReferenceValidationContext~` : receives

    class IValidatorReceiverSelector {
        With()
        AfterAllAspects()
        BeforeAnyAspect()
    }

    IValidatorReceiver <-- IValidatorReceiverSelector : creates
    IValidatorReceiverSelector <-- IValidatorReceiverSelector

    IValidatorReceiverSelector <|-- IAspectBuilder : derives from
    IValidatorReceiverSelector <|-- IAmender : derives from
    IAspectBuilder <-- IAspect : receives
    IAmender <-- Fabric : receives

Namespace Members

Classes

DeclarationValidationContext

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

ReferenceKinds

Enumerates all kinds of references.

ReferenceValidationContext

The context object passed to the single parameter of validators added using ValidateReferences<TValidator>(Func<TDeclaration, TValidator>).

ReferenceValidator

A validator that can validate code references.

Validator<TContext>

A base class for validators.

ValidatorDelegate<TContext>

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

Interfaces

IValidatorReceiver

The non-generic base interface for IValidatorReceiver<TDeclaration>. Represents a set of declarations to which validators, diagnostics and code fix suggestions can be added.

IValidatorReceiver<TDeclaration>

Represents a set of declarations to which validators, diagnostics, code fix suggestions, and options can be added.

IValidatorReceiverSelector<TTarget>

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