Open sandboxFocus

Method Override

Override(IAdviser<IMethod>, in MethodTemplateSelector, object?, object?)

Overrides a method's implementation with code from a template.

Declaration
public static IOverrideAdviceResult<IMethod> Override(this IAdviser<IMethod> adviser, in MethodTemplateSelector template, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<IMethod> adviser

An adviser for a method.

MethodTemplateSelector template

The template that defines the new method implementation. This can be either:

  • A string with the name of a single template method in the aspect class
  • A MethodTemplateSelector to select different templates based on the target method's characteristics (async, iterator, etc.)
Template methods must be annotated with TemplateAttribute.
object args

An optional object (typically of anonymous type) whose properties map to template method parameters or type parameters. See Sharing state with adviceSharing state with advice for details.

object tags

An optional object (typically of anonymous type) passed to the template and accessible via meta.Tags, useful for passing context or configuration. See Sharing state with adviceSharing state with advice for details.

Returns
Type Description
IOverrideAdviceResult<IMethod>

An IOverrideAdviceResult<T> providing access to the overridden method declaration.

Remarks

This is the programmatic way to override methods from BuildAspect(IAspectBuilder<T>). For a simplified approach when the aspect's only purpose is to override a method, derive from OverrideMethodAspect instead.

Within the template, use meta.Proceed() to invoke the original method implementation, and meta.Target.Method to access metadata about the target method (name, parameters, return type, attributes). Access parameter values via meta.Target.Parameters (e.g., meta.Target.Parameters[0].Value or meta.Target.Parameters["paramName"].Value).

For async and iterator methods, the default template automatically adapts meta.Proceed() to match the method's signature (e.g., adding await for async methods, buffering for iterators). For fine-grained control over async or iterator behavior, use MethodTemplateSelector to specify specialized templates that avoid buffering or enable use of await/yield in the template code.

Use With<TNewDeclaration>(TNewDeclaration) to override a different method than the current target.

See Also

Override(IAdviser<IConstructor>, string, object?, object?)

Overrides the implementation of a constructor. Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to a different constructor than the current one.

Declaration
public static IOverrideAdviceResult<IConstructor> Override(this IAdviser<IConstructor> adviser, string template, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<IConstructor> adviser

An adviser for a constructor.

string template

Name of a template method in the aspect class. This method must be annotated with TemplateAttribute.

object args

An optional object (typically of anonymous type) whose properties map to template method parameters or type parameters. See Sharing state with adviceSharing state with advice for details.

object tags

An optional object (typically of anonymous type) passed to the template and accessible via meta.Tags. See Sharing state with adviceSharing state with advice for details.

Returns
Type Description
IOverrideAdviceResult<IConstructor>

An IOverrideAdviceResult<T> exposing the overridden IConstructor.

See Also

Override(IAdviser<IFieldOrProperty>, string, object?)

Overrides a field or property by specifying a property template. Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to a different field or property than the current one.

Declaration
public static IOverrideAdviceResult<IProperty> Override(this IAdviser<IFieldOrProperty> adviser, string template, object? tags = null)
Parameters
Type Name Description
IAdviser<IFieldOrProperty> adviser

An adviser for a field or property.

string template

The name of a template property in the aspect class. This property must be annotated with TemplateAttribute. The template property's accessors (get/set) define which accessors are overridden in the target.

object tags

An optional object (typically of anonymous type) passed to the template and accessible via meta.Tags. See Sharing state with adviceSharing state with advice for details.

Returns
Type Description
IOverrideAdviceResult<IProperty>

An IOverrideAdviceResult<T> exposing the overridden IProperty.

Remarks

This is a simplified overload for overriding fields or properties using a template property. For an even simpler approach when the aspect's only purpose is to override a field or property, use OverrideFieldOrPropertyAspect instead. For fine-grained control over individual accessors (e.g., overriding only the getter), use OverrideAccessors(IAdviser<IFieldOrPropertyOrIndexer>, in GetterTemplateSelector, string?, object?, object?) instead.

When applied to a field or auto-property, a backing field is automatically introduced. Within the template property, access the underlying value via meta.Target.FieldOrProperty.Value.

See Also