Open sandboxFocus

Method OverrideAccessors

OverrideAccessors(IAdviser<IFieldOrPropertyOrIndexer>, in GetterTemplateSelector, string?, object?, object?)

Overrides a field, property, or indexer by specifying templates for the getter and/or setter. Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to a different field, property or indexer than the current one.

Declaration
public static IOverrideAdviceResult<IPropertyOrIndexer> OverrideAccessors(this IAdviser<IFieldOrPropertyOrIndexer> adviser, in GetterTemplateSelector getTemplate = default, string? setTemplate = null, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<IFieldOrPropertyOrIndexer> adviser

An adviser for a field, property or indexer.

GetterTemplateSelector getTemplate

The name of the method of the aspect class whose implementation will be used as a template for the getter, or null if the getter should not be overridden. This method must be annotated with TemplateAttribute. The signature of this method must be T Get() where T is either dynamic or compatible with the field, property or indexer type. To select different templates for iterator getters (IEnumerable/IEnumerator), use GetterTemplateSelector to avoid buffering.

string setTemplate

The name of the method of the aspect class whose implementation will be used as a template for the setter, or null if the setter should not be overridden. This method must be annotated with TemplateAttribute. The signature of this method must be void Set(T value) where T is either dynamic or compatible with the field, property or indexer type.

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<IPropertyOrIndexer>

An IOverrideAdviceResult<T> exposing the overridden IPropertyOrIndexer.

Remarks

This method provides fine-grained control over accessor overriding by using separate method templates for getters and setters. Within templates, use meta.Target.FieldOrProperty.Value (for fields/properties) or meta.Target.Indexer[...].Value (for indexers) to access the underlying value, and meta.Proceed() to invoke the original accessor.

When applied to a field or auto-property, a backing field is automatically introduced.

See Also

OverrideAccessors(IAdviser<IFieldOrProperty>, in GetterTemplateSelector, string?, object?, object?)

Overrides a field or property by specifying a method template for the getter, the setter, or both. Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to another field or property than the current one.

Declaration
public static IOverrideAdviceResult<IProperty> OverrideAccessors(this IAdviser<IFieldOrProperty> adviser, in GetterTemplateSelector getTemplate = default, string? setTemplate = null, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<IFieldOrProperty> adviser

An adviser for a field or property.

GetterTemplateSelector getTemplate

The name of the method of the aspect class whose implementation will be used as a template for the getter, or null if the getter should not be overridden. This method must be annotated with TemplateAttribute. The signature of this method must be T Get() where T is either dynamic or a type compatible with the type of the field or property. To select different templates for iterator getters, use the constructor of the GetterTemplateSelector type. To specify a single template for all properties, pass a string.

string setTemplate

The name of the method of the aspect class whose implementation will be used as a template for the setter, or null if the setter should not be overridden. This method must be annotated with TemplateAttribute. The signature of this method must be void Set(T value) where T is either dynamic or a type compatible with the type of the field or property.

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<IProperty>

An IOverrideAdviceResult<T> exposing the overridden IProperty.

See Also

OverrideAccessors(IAdviser<IIndexer>, in GetterTemplateSelector, string?, object?, object?)

Overrides an indexer by specifying a method template for the getter, the setter, or both. Use the With<TNewDeclaration>(TNewDeclaration) method to apply the advice to another indexer than the current one.

Declaration
public static IOverrideAdviceResult<IIndexer> OverrideAccessors(this IAdviser<IIndexer> adviser, in GetterTemplateSelector getTemplate = default, string? setTemplate = null, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<IIndexer> adviser

An adviser for an indexer.

GetterTemplateSelector getTemplate

The name of the method of the aspect class whose implementation will be used as a template for the getter, or null if the getter should not be overridden. This method must be annotated with TemplateAttribute. The signature of this method must be T Get() where T is either dynamic or a type compatible with the type of the indexer. To select a different templates for iterator getters, use the constructor of the GetterTemplateSelector type. To specify a single template for all indexers, pass a string.

string setTemplate

The name of the method of the aspect class whose implementation will be used as a template for the setter, or null if the setter should not be overridden. This method must be annotated with TemplateAttribute. The signature of this method must be void Set(T value) where T is either dynamic or a type compatible with the type of the indexer.

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<IIndexer>

An IOverrideAdviceResult<T> exposing the overridden IIndexer.

See Also

OverrideAccessors(IAdviser<IEvent>, string?, string?, string?, string?, object?, object?)

Overrides an event by specifying a template for the adder, the remover, and/or the invocation interception. Use the With<TNewDeclaration>(TNewDeclaration) method to override a different event than the current one.

Declaration
public static IOverrideAdviceResult<IEvent> OverrideAccessors(this IAdviser<IEvent> adviser, string? addTemplate = null, string? removeTemplate = null, string? raiseTemplate = null, string? invokeTemplate = null, object? args = null, object? tags = null)
Parameters
Type Name Description
IAdviser<IEvent> adviser

An adviser for an event.

string addTemplate

The name of the method of the aspect class whose type and implementation will be used as a template for the adder, or null if the adder should not be overridden. This method must be annotated with TemplateAttribute. The signature of this method must be void Add(T value) where T is either dynamic or a type compatible with the type of the event.

string removeTemplate

The name of the method of the aspect class whose type and implementation will be used as a template for the remover, or null if the remover should not be overridden. This method must be annotated with TemplateAttribute. The signature of this method must be void Remove(T value) where T is either dynamic or a type compatible with the type of the event.

string raiseTemplate

Reserved for future use (not implemented).

string invokeTemplate

The name of the method of the aspect class whose type and implementation will be used as a template for intercepting invocation of the event's handlers. This template is invoked once per event handler when the event is raised. The signature of this method must be T Invoke(), T Invoke(U handler), or T Invoke(U handler, V1 param1, V2 param2, ...) where T is either dynamic or a type compatible with the return value of the event's delegate type, U is either dynamic or the event's delegate type, Vn are types matching the delegate's parameters. Use meta.Proceed() within the template to invoke the actual handler.

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<IEvent>

An IOverrideAdviceResult<T> exposing the overridden IEvent.

Remarks

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

When applied to a field-like event, it is automatically transformed into an explicitly implemented event with a backing field, similar to how auto-properties are transformed.

The invokeTemplate provides powerful per-handler interception capabilities. Since it's invoked once per handler, you can implement patterns like exception handling with handler removal, async execution, or handler filtering. Within the template, access event metadata via meta.Target.Event and programmatically add/remove handlers using meta.Target.Event.Add(handler) and meta.Target.Event.Remove(handler).

Performance Note: Overriding the invoke operation uses an EventBroker<TDelegate, TArgs, TState> pattern which adds memory overhead (one broker instance per event per object instance) and allocates memory during event invocation. This may affect performance for high-frequency events.

Limitations: Delegate signatures with non-void return types or with ref/out parameters are not supported. Only handlers added through the event's add/remove accessors will be intercepted.

See Also