Open sandboxFocus

Class OverrideEventAspect

A base aspect that overrides the implementation of an event by providing template methods for add, remove, and invoke operations.

Inheritance
OverrideEventAspect
Namespace: Metalama.Framework.Aspects
Assembly: Metalama.Framework.dll
Syntax
[AttributeUsage(AttributeTargets.Event)]
public abstract class OverrideEventAspect : EventAspect, IAspect<IEvent>, IAspect, ICompileTimeSerializable, ITemplateProvider, IEligible<IEvent>
Remarks

This class simplifies creating aspects that override event behavior. Derived classes can override the template methods OverrideAdd(dynamic), OverrideRemove(dynamic), and OverrideInvoke(dynamic) to customize event semantics. All template methods are optional (marked with [Template(IsEmpty = true)]).

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 when overridden.

The OverrideInvoke(dynamic) template is invoked once per event handler when the event is raised. For example, if there are 3 event handlers and the event is invoked once, OverrideInvoke(dynamic) will be called 3 times. This enables per-handler interception patterns such as exception handling with handler removal, async execution, or handler filtering. Use meta.Proceed() to invoke the actual handler. You can remove handlers from within the template using meta.Target.Event.Remove(handler).

Limitations: Delegate signatures with non-void return types or with ref/out parameters are not supported. Only handlers added through the event's add and remove accessors are intercepted; handlers added directly to the backing field (if accessible) will not be intercepted.

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, unless the event is static) and allocates short-term memory during event invocation. Additional type conversions (casts) are also required. This may affect performance for high-frequency events, although high-frequency events are not a common use case for .NET events.

Constructors

Name Description
OverrideEventAspect()

Methods

Name Description
BuildAspect(IAspectBuilder<IEvent>)

Defines the aspect implementation by adding advice, child aspects, and validators to the target declaration.

BuildEligibility(IEligibilityBuilder<IEvent>)

Configures the eligibility of the aspect or attribute by defining rules that determine which declarations the aspect can be applied to.

OverrideAdd(dynamic)

Template method for overriding the event add accessor.

OverrideInvoke(dynamic)

Template method for overriding event invocation behavior, executed once per handler when the event is raised.

OverrideRemove(dynamic)

Template method for overriding the event remove accessor.

Extension Methods

See Also