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 MethodTemplateSelectorRemarks
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:
- AsyncTemplate: Methods with the
asyncmodifier - EnumerableTemplate/EnumeratorTemplate: Methods using
yieldstatements - AsyncEnumerableTemplate/AsyncEnumeratorTemplate: Async iterator methods (both
asyncandyield) - DefaultTemplate: All other methods (required)
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 |
| AsyncEnumeratorTemplate | Gets the name of the template that must be applied to an async iterator method returning the |
| 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 |
| 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 |
| 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 |
| UseAsyncTemplateForAnyAwaitable | Gets a value indicating whether the AsyncTemplate must be applied to all methods returning an awaitable type (including |
| 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 |
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. |