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:
<ul><li><xref href="Metalama.Framework.Aspects.OverrideMethodAspect.OverrideAsyncMethod" data-throw-if-not-resolved="false"></xref> for <code>async</code> methods (or all awaitable types if <xref href="Metalama.Framework.Aspects.OverrideMethodAspect.UseAsyncTemplateForAnyAwaitable" data-throw-if-not-resolved="false"></xref> is <code>true</code>)</li><li><xref href="Metalama.Framework.Aspects.OverrideMethodAspect.OverrideEnumerableMethod" data-throw-if-not-resolved="false"></xref> or <xref href="Metalama.Framework.Aspects.OverrideMethodAspect.OverrideEnumeratorMethod" data-throw-if-not-resolved="false"></xref> for iterator methods using <code>yield</code> (or all enumerable types if <xref href="Metalama.Framework.Aspects.OverrideMethodAspect.UseEnumerableTemplateForAnyEnumerable" data-throw-if-not-resolved="false"></xref> is <code>true</code>)</li><li><code>OverrideAsyncEnumerableMethod</code> or <code>OverrideAsyncEnumeratorMethod</code> for async iterator methods (or all async enumerable types if both properties are <code>true</code>)</li><li><xref href="Metalama.Framework.Aspects.OverrideMethodAspect.OverrideMethod" data-throw-if-not-resolved="false"></xref> for all other methods</li></ul>
If specialized templates are not overridden, <xref href="Metalama.Framework.Aspects.OverrideMethodAspect.OverrideMethod" data-throw-if-not-resolved="false"></xref> is used as the fallback.
</p>
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() | Template of the new method implementation for methods returning IAsyncEnumerable<T>. By default, this template is used for methods using the |
| OverrideAsyncEnumeratorMethod() | Template of the new method implementation for methods returning IAsyncEnumerator<T>. By default, this template is used for methods using the |
| 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. |