Open sandboxFocus

Class OverrideMethodAspect

A base class for aspects that override method implementations using T# templates.

Namespace: Metalama.Framework.Aspects
Assembly: Metalama.Framework.dll
Syntax
[AttributeUsage(AttributeTargets.Method)]
public abstract class OverrideMethodAspect : MethodAspect, IAspect<IMethod>, IAspect, ICompileTimeSerializable, ITemplateProvider, IEligible<IMethod>
Remarks

This class simplifies creating aspects that replace method implementations using the decorator pattern. Derived classes must implement the OverrideMethod() template method, which is a T# template that executes at compile-time to generate the new method implementation. Use meta.Proceed() within the template to invoke the original method implementation, and meta.Target.Method to access method metadata.

This aspect automatically selects the appropriate template based on the target method's characteristics:

If specialized templates are not overridden, OverrideMethod() is used as the fallback.

Important: When the default OverrideMethod() template is applied to iterator methods, the stream is buffered into a List<T> using Buffer(IEnumerable). For long-running or infinite streams, implement the specific iterator templates (OverrideEnumerableMethod() or OverrideEnumeratorMethod()) to avoid buffering and support streaming.

The properties UseAsyncTemplateForAnyAwaitable and UseEnumerableTemplateForAnyEnumerable control template selection strategy. By default (false), templates are selected based on method modifiers (async, yield). When set to true, selection is based solely on return type, which is useful when you need to handle all methods of certain return types consistently.

For overriding multiple methods from a single aspect or for more control, use Override(IAdviser<IMethod>, in MethodTemplateSelector, object?, object?) from your BuildAspect method instead of deriving from this class.

Constructors

Name Description
OverrideMethodAspect()

Properties

Name Description
UseAsyncTemplateForAnyAwaitable

Gets or sets a value indicating whether async templates should be selected based on return type rather than the async modifier.

UseEnumerableTemplateForAnyEnumerable

Gets or sets a value indicating whether enumerable templates should be selected based on return type rather than the yield modifier.

Methods

Name Description
BuildAspect(IAspectBuilder<IMethod>)

Defines the aspect implementation by adding advice, child aspects, and validators to the target declaration.

BuildEligibility(IEligibilityBuilder<IMethod>)

Configures the eligibility of the aspect or attribute by defining rules that determine which declarations the aspect can be applied to.

OverrideAsyncEnumerableMethod()
OverrideAsyncEnumeratorMethod()
OverrideAsyncMethod()

Template for overriding asynchronous methods.

OverrideEnumerableMethod()

Template of the new method implementation for methods returning IEnumerable<T>. By default, this template is used for methods using the yield statement. When UseEnumerableTemplateForAnyEnumerable is true, this template is used for all methods returning IEnumerable<T>. If this template is not overridden, OverrideMethod() is used instead.

OverrideEnumeratorMethod()

Template of the new method implementation for methods returning IEnumerator<T>. By default, this template is used for methods using the yield statement. When UseEnumerableTemplateForAnyEnumerable is true, this template is used for all methods returning IEnumerator<T>. If this template is not overridden, OverrideMethod() is used instead.

OverrideMethod()

The default template for overriding method implementations.

Extension Methods

See Also