Open sandboxFocus

Struct MethodTemplateSelector

Specifies which T# templates to use when overriding methods, enabling automatic selection of specialized templates based on the target method's characteristics (async, iterator, async iterator).

Namespace: Metalama.Framework.Advising
Assembly: Metalama.Framework.dll
Syntax
[CompileTime]
public readonly struct MethodTemplateSelector
Remarks

When overriding methods with Override(IAdviser<IMethod>, in MethodTemplateSelector, object?, object?), different target methods may require different template implementations. For example, an async method might need a template that uses await meta.ProceedAsync(), while an iterator method might need yield return semantics.

MethodTemplateSelector allows you to specify multiple templates and let Metalama automatically select the appropriate one. Template selection depends on two factors: the method's characteristics and the UseAsyncTemplateForAnyAwaitable and UseEnumerableTemplateForAnyEnumerable flags.

Default behavior (both flags are false): Templates are selected based on how the method is implemented:

When UseAsyncTemplateForAnyAwaitable is true: AsyncTemplate is selected based on the method's return type being awaitable (e.g., Task, ValueTask), regardless of whether the method has the async modifier.

When UseEnumerableTemplateForAnyEnumerable is true: Iterator templates are selected based on the method's return type (e.g., IEnumerable<T>, IAsyncEnumerable), regardless of whether the method uses yield statements.

This type has an implicit conversion from string, so if you only need a default template, you can pass the template name directly without constructing a MethodTemplateSelector.

Constructors

Name Description
MethodTemplateSelector(string, string?, string?, string?, string?, string?, bool, bool)

Initializes a new instance of the MethodTemplateSelector struct by specifying the name of the template methods to be applied. The named passed to this constructor must be the name of methods of the current aspect class, and these methods must be annotated with the TemplateAttribute custom attribute. You can define several templates by passing a value to optional parameters. The appropriate template will be automatically selected according to the method to which the advice is applied. If several templates are eligible for a method, the template that is the last in the list of parameters is selected.

Properties

Name Description
AsyncEnumerableTemplate

Gets the name of the template that must be applied to an async iterator method returning the IAsyncEnumerable type. If the UseEnumerableTemplateForAnyEnumerable is set to true, this template will be used for any method that returns the IAsyncEnumerable type, even if it is not implemented as an async or yield-based iterator.

AsyncEnumeratorTemplate

Gets the name of the template that must be applied to an async iterator method returning the IAsyncEnumerable type. If the UseEnumerableTemplateForAnyEnumerable is set to true, this template will be used for any method that returns the IAsyncEnumerable type, even if it is not implemented as an async or yield-based iterator.

AsyncTemplate

Gets the name of the template that must be applied to async methods, including async iterators (unless AsyncEnumerableTemplate or AsyncEnumeratorTemplate is defined). If UseAsyncTemplateForAnyAwaitable is set to true, this template will be used for any method that has an awaitable return type, including IAsyncEnumerable and IAsyncEnumerator.

DefaultTemplate

Gets the name of the template that must be applied if no other template is applicable. This property is required.

EnumerableTemplate

Gets the name of the template that must be applied to yield-based iterator methods returning an IEnumerable<T> or IEnumerable. If the UseEnumerableTemplateForAnyEnumerable is set to true, this template will be used for any method that returns the IEnumerable<T> or IEnumerable type, even if it not a yield-based iterator.

EnumeratorTemplate

Gets the name of the template that must be applied to yield-based iterator methods returning an IEnumerator<T> or IEnumerator. If the UseEnumerableTemplateForAnyEnumerable is set to true, this template will be used for any method that returns the IEnumerator<T> or IEnumerator type, even if it not a yield-based iterator.

UseAsyncTemplateForAnyAwaitable

Gets a value indicating whether the AsyncTemplate must be applied to all methods returning an awaitable type (including IAsyncEnumerable and IAsyncEnumerator) instead of only to methods that have the async modifier. If the implementation of the template method is async, the awaitable type must also have an async method builder, otherwise the method will be processed by DefaultTemplate.

UseEnumerableTemplateForAnyEnumerable

Gets a value indicating whether the EnumerableTemplate, EnumeratorTemplate, AsyncEnumerableTemplate, AsyncEnumeratorTemplate must be applied to all methods returning a compatible return type (if set to true), instead of only to methods using the yield statement.

Operators

Name Description
implicit operator MethodTemplateSelector(string)

Converts a string to a new instance of the MethodTemplateSelector where the DefaultTemplate property is set to this string.

See Also