Open sandboxFocusImprove this doc

Namespace Metalama.Framework.Aspects

This namespace enables you to build aspects. Aspects represent an algorithmic approach to code transformation or validation.

For instance, tasks such as adding logging to a method or implementing INotifyPropertyChanged can largely be expressed as an algorithm and, therefore, implemented as an aspect.

Conceptual Documentation

Refer to Creating aspects for more information.

Overview

To create an aspect, you need to create a class that derives from Attribute and implement the IAspect<T> interface. Alternatively, you can use one of the following classes, which already derive from Attribute, have the appropriate AttributeUsageAttribute, and implement the IAspect<T> interface:

Class Diagrams

Aspect builders

classDiagram

    class IAspect {
        BuildAspect(IAspectBuilder)
        BuildEligibility(IEligibilityBuilder)
    }

    class IAspectBuilder {
        SkipAspect()
        TargetDeclaration
    }

    class IAdviser {
        Target
        With(declaration)
    }


    class ScopedDiagnosticSink {
        Report(...)
        Suppress(...)
        Suggest(...)
    }

    class AdviserExtensions {
        <<static>>
        Override(...)
        Introduce*(...)
        ImplementInterface(...)
        AddContract(...)
        AddInitializer(...)
    }

    class IAspectReceiver {
        Select(...)
        SelectMany(...)
        Where(...)
        AddAspect(...)
        AddAspectIfEligible(...)
        Validate(...)
        ValidateInboundReferences(...)
        ReportDiagnostic(...)
        SuppressDiagnostic(...)
        SuggestCodeFix(...)
    }

    IAspect --> IAspectBuilder : BuildAspect() receives
    IAspectBuilder --|> IAdviser : inherits 
    IAspectBuilder --> ScopedDiagnosticSink : exposes
    IAspectBuilder --> IAspectReceiver : exposes
    AdviserExtensions --> IAdviser : provides extension\nmethods

Scope custom attributes

classDiagram

ScopeAttribute <|-- CompileTimeAttribute  : derives from
ScopeAttribute <|-- RunTimeOrCompileTimeAttribute  : derives from


Advice and template attributes

classDiagram

IAdviceAttribute <|-- ITemplateAttribute : derives from
IAdviceAttribute <| -- DeclarativeAdviceAttribute : derives from
ITemplateAttribute <|-- TemplateAttribute  : derives from
DeclarativeAdviceAttribute <|-- IntroduceAttribute  : derives from
DeclarativeAdviceAttribute <|-- IntroduceDependencyAttribute  : derives from
ITemplateAttribute <|-- InterfaceMemberAttribute  : derives from

IAspectInstance, IAspectPredecessor

The IAspectPredecessor facility allows aspects to access their parent (i.e. the artifact that created them): a parent aspect, a fabric, or a custom attribute.

classDiagram

   class AspectPredecessorKind {
       <<enum>>
       Attribute
       ChildAspect
       RequiredAspect
       Inherited
       Fabric
   }

    class IAspect {
        BuildAspect(IAspectBuilder)
    }

    class IAspectBuilder {
        AspectInstance
    }

    class  IAspectState {

    }

    class IAttribute {
        ContainingDeclaration
        Type
        Constructor
        ConstructorArguments
        NamedArguments
    }

    IAspectInstance <-- IAspectBuilder : exposes

    IAspectBuilder <-- IAspect : receives

    IAspectPredecessor <|-- IAttribute : derives from

    IAspectPredecessor <|-- IFabricInstance : derives from

    class IAspectPredecessor {

    }

    class IFabricInstance {
        Fabric
        TargetDeclaration
    }

   class IAspectInstance {
       Aspect
       AspectClass
       IsSkipped
       Procecessors
       SecondaryInstances
       State
       TargetDeclaration
   }

    IAspectState <-- IAspectInstance : exposes
    IAspect <-- IAspectInstance : exposes
    IAspectPredecessor <|-- IAspectInstance : derives from
    IAspectState <-- IAspect : reads & writes

   IAspectInstance *-- AspectPredecessor : has

   class AspectPredecessor {
       Kind
       Instance
   }

   IAspectPredecessor <-- AspectPredecessor : exposes
   AspectPredecessorKind <-- AspectPredecessor : has

Namespace members

Classes

Aspect

A base class from aspects that can be applied as custom attributes. Aspects must implement a specific generic instance of the IAspect<T> interface, or derive from TypeAspect, MethodAspect, ConstructorAspect, FieldOrPropertyAspect, EventAspect or CompilationAspect.

AspectOrderAttribute

Custom attribute that specifies the order of execution of aspects or aspect layers.

AspectPredecessorExtensions

Extension methods for IAspectPredecessor.

AspectReceiverExtensions

Extension methods for IAspectReceiver<TDeclaration>.

CompilationAspect

A base class for aspects that can be applied the compilation as custom attributes (using the [assembly: MyAspect] syntax).

CompileTimeAttribute

Attribute that means that the target declaration (and all children declarations) can only be called from compile-time code and, therefore, not from run-time code. See RunTimeOrCompileTimeAttribute for declarations that can be called both from compile-time and run-time code.

CompiledTemplateAttribute

This custom attribute is internal to the Metalama infrastructure and should not be used in user code. It is added by Metalama when an aspect is compile to store the original characteristics of the template because some are changed during compilation.

ConstructorAspect

A base class for aspects that can be applied to constructors as custom attributes.

ContractAspect

A base aspect that can validate or change the value of fields, properties, indexers, and parameters.

DeclarativeAdviceAttribute

A base class for attributes that define declarative advice members.

EditorExperienceAttribute

EditorExperienceOptions

EventAspect

A base class for aspects that can be applied to events as custom attributes.

ExcludeAspectAttribute

Custom attribute attribute that, when applied to a declaration, specifies that this declaration and all its members must not be the target of aspects of given types, unless the aspect is explicitly specified as a custom attribute.

FieldAspect

A base class for aspects that can be applied to fields as custom attributes.

FieldOrPropertyAspect

A base class for aspects that can be applied to fields or properties as custom attributes.

FrameworkDiagnosticDescriptors

InheritableAttribute

Custom attribute that, when applied to an aspect class, means that instances of this aspect are inherited from the base class or interface to derived classes, from base methods to method overrides, from interface methods to method implementations, and so on.

InterfaceMemberAttribute

Custom attribute that, when applied to a member of an aspect class, means that this aspect member is a template implementing a member of an interface implemented by ImplementInterface(INamedType, INamedType, OverrideStrategy, object?).

IntroduceAttribute

Custom attribute that can be applied to any member of an aspect class and that means that this member must be introduced to the target class of the aspect.

InvalidAdviceParametersException

InvalidTemplateSignatureException

An exception thrown by IAdviceFactory when compile-time code attempts to add a template to a target declaration and the template is not compatible with the advice and the target declaration.

LayersAttribute

Custom attribute that, when applied to an aspect class, means that this aspect class uses several layers, and defines the name and the order of execution of these layers. In multi-aspect layers, the BuildAspect(IAspectBuilder<T>) method is called several times, once for each layer. The current layer is exposed in the Layer property.

MethodAspect

A base class for aspects that can be applied to methods as custom attributes.

OverrideEventAspect

A base aspect that overrides the implementation of an event.

OverrideFieldOrPropertyAspect

A base aspect that overrides the implementation of a field or a property.

OverrideMethodAspect

A base aspect that overrides the implementation of a method.

ParameterAspect

A base class for aspects that can be applied to parameters as custom attributes.

PropertyAspect

A base class for aspects that can be applied to properties as custom attributes.

RequireAspectWeaverAttribute

Custom attribute that, when applied an an aspect class, means that this aspect class is implemented by a low-level weaver built with Metalama SDK. When the RequireAspectWeaverAttribute is added to a type, the BuildAspect(IAspectBuilder<T>) method is not invoked.

RunTimeAttribute

Attribute that means that the target declaration (and all children declarations) can only be called from run-time code and, therefore, not from compile-time code. Code is run-time by default, so this attribute only makes sense on classes or interface that are run-time-only but derive a run-time-or-compile-time type. See RunTimeOrCompileTimeAttribute.

RunTimeOrCompileTimeAttribute

Attribute that means that the target declaration (and all children declarations) can be called both from compile-time and run-time code. See CompileTimeAttribute for declarations that cannot be called from run-time code.

ScopeAttribute

A base class for all custom attributes that influence the scope (compile-time or run-time) of the code or its role in an aspect.

TemplateAttribute

The base class for all custom attributes that mark a declaration as a template.

TemplateInvocation

Represents call to a template method.

ThisAttribute

When applied to a template method parameter, indicates that the introduced parameter should have the this modifier, and that the introduced method should be an extension method.

TypeAspect

A base class for aspects that can be applied to types as custom attributes.

TypeParameterAspect

A base class for aspects that can be applied to type parameters (i.e. generic parameters) as custom attributes.

meta

The entry point for the meta model, which can be used in templates to inspect the target code or access other features of the template language.

Structs

AspectPredecessor

Represents the relationship that an object (attribute, fabric, aspect) has created or required another aspect or validator. These relationships are exposed on Predecessors.

TemplateProvider

Wraps a template provider type or instance.

Interfaces

IAspect

The base interface for all aspects. A class should not implement this interface, but the strongly-typed variant IAspect<T>.

IAspectBuilder

An object used by the BuildAspect(IAspectBuilder<T>) method of the aspect to provide advice, child aspects and validators, or report diagnostics. This is the weakly-typed variant of the IAspectBuilder<TAspectTarget> interface.

IAspectBuilder<TAspectTarget>

An object used by the BuildAspect(IAspectBuilder<T>) method of the aspect to provide advice, child aspects and validators, or report diagnostics. This is a strongly-typed variant of the IAspectBuilder interface.

IAspectClass

Represents the metadata of an aspect class.

IAspectInstance

Represents an instance of an aspect. The instance of the IAspect itself is in the Aspect property.

IAspectPredecessor

Base interface for objects that can cause aspects to be added to a compilation. Predecessors are exposed on the Predecessors property.

IAspectReceiver<TDeclaration>

Represents a set of declarations and offers the ability to add aspects and set options to them. It inherits from IValidatorReceiver<TDeclaration>, which allows to add validators.

IAspectReceiver<TDeclaration, TTag>

IAspectState

An empty interface that must be implemented by objects assigned to the AspectState property of the IAspectBuilder interface.

IAspect<T>

The base interface for all aspects, with the type parameter indicating to which types of declarations the aspect can be added.

IConditionallyInheritableAspect

An interface that can be implemented by aspect that can be inheritable or non-inheritable based of a field or property of the aspect. When all the instances of the aspect class are unconditionally inheritable, the class must be annotated with the InheritableAttribute instead.

IMetaTarget

IObjectReader

Exposes as a dictionary the tags passed to an advise.

ITemplateProvider

An interface that specifies that the type contains templates. Templates must be annotated with TemplateAttribute.

Enums

AspectOrderDirection

Specifies the order in which the aspect types or aspect layers are supplied to AspectOrderAttribute.

AspectPredecessorKind

Kinds of AspectPredecessor.

ContractDirection

Directions of the data flow to which the contract applies.

InterfaceMemberOverrideStrategy

Member conflict behavior of interface introduction advice.

IntroductionScope

Scope of introduction advice.

OverrideStrategy

Conflict behavior of introduction advice.