Open sandboxFocus

Class Aspect

A base class for aspects that can be applied as custom attributes. Derive from this class or a specialized aspect base class to create aspects that transform or validate code at compile time.

Namespace: Metalama.Framework.Aspects
Assembly: Metalama.Framework.dll
Syntax
public abstract class Aspect : Attribute, IAspect, ICompileTimeSerializable, ITemplateProvider
Remarks

This class derives from Attribute, allowing aspects to be applied to code elements using C# attribute syntax such as [MyAspect]. Aspect classes must also implement a specific generic instance of the IAspect<T> interface, or derive from a specialized base class like TypeAspect, MethodAspect, FieldOrPropertyAspect, EventAspect, ConstructorAspect, or CompilationAspect.

Specialized base classes for common scenarios: Instead of implementing BuildAspect(IAspectBuilder<T>) yourself, consider using specialized base classes that already implement BuildAspect(IAspectBuilder<T>) and provide simpler approaches based on virtual methods. For overriding members, use OverrideMethodAspect, OverrideFieldOrPropertyAspect, OverrideEventAspect, or ContractAspect. For aspects that target specific declaration kinds, use TypeAspect, MethodAspect, or similar. Derive directly from Aspect only when you need a custom attribute applicable to multiple declaration kinds (e.g., both methods and fields).

This class is a convenience base class. The aspect framework primarily requires implementation of the IAspect<T> interface. You can create aspects that don't derive from Attribute if you only apply them programmatically via fabrics.

How to create an aspect:

  1. Derive from a specialized aspect class (e.g., OverrideMethodAspect, TypeAspect) or this base class
  2. Override the BuildAspect(IAspectBuilder<T>) method to add advice, child aspects, or validations (not needed for override aspects)
  3. Optionally override BuildEligibility(IEligibilityBuilder<T>) to define where the aspect can be applied
  4. Define template methods and annotate them with TemplateAttribute for code generation
  5. Optionally annotate the aspect class with LayersAttribute to split BuildAspect(IAspectBuilder<T>) into multiple execution layers

Constructors

Name Description
Aspect()

Methods

Name Description
ToString()

Extension Methods

See Also