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:
- OverrideAsyncMethod() for
asyncmethods (or all awaitable types if UseAsyncTemplateForAnyAwaitable istrue) - OverrideEnumerableMethod() or OverrideEnumeratorMethod() for iterator methods using
yield(or all enumerable types if UseEnumerableTemplateForAnyEnumerable istrue) OverrideAsyncEnumerableMethodorOverrideAsyncEnumeratorMethodfor async iterator methods (or all async enumerable types if both properties aretrue)- OverrideMethod() for all other methods
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 |
| UseEnumerableTemplateForAnyEnumerable | Gets or sets a value indicating whether enumerable templates should be selected based on return type rather than the |
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 |
| OverrideEnumeratorMethod() | Template of the new method implementation for methods returning IEnumerator<T>. By default, this template is used for methods using the |
| OverrideMethod() | The default template for overriding method implementations. |