Open sandboxFocus

Class EventInterceptionAspect

Aspect that, when applied on an event, intercepts invocations of its semantics Add (OnAddHandler(EventInterceptionArgs)), Remove (OnRemoveHandler(EventInterceptionArgs)) and Invoke (OnInvokeHandler(EventInterceptionArgs)).

Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
Syntax
[HasInheritedAttribute]
[MulticastAttributeUsage(MulticastTargets.Event, AllowMultiple = true, PersistMetaData = false, TargetMemberAttributes = MulticastAttributes.NonAbstract)]
[AttributeUsage(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Event|AttributeTargets.Interface, AllowMultiple = true)]
[AspectConfigurationAttributeType(typeof(EventInterceptionAspectConfigurationAttribute))]
[Serializer(null)]
public abstract class EventInterceptionAspect : EventLevelAspect, IEventLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, IEventInterceptionAspect, IEventLevelAspect, IAspect
Remarks

Applying an aspect of type EventInterceptionAspect to an event results in the add and remove methods of the event to be replaced by a call to OnAddHandler(EventInterceptionArgs) and OnRemoveHandler(EventInterceptionArgs), respectively. The original accessor body is moved into new methods, which the aspect code can call by invoking ProceedAddHandler() or ProceedRemoveHandler().

The method OnInvokeHandler(EventInterceptionArgs), if defined, is called instead when the event is raised, once for every handler that has been added to the list. When you define the OnInvokeHandler(EventInterceptionArgs) method, PostSharp will use a broker object to control access to the event. This broker object will be the unique subscriber of the event, and it maintains its own list of subscribers.

note

The aspect works only when the caller code accesses the event through its add/remove semantics. It does not work when the caller code bypasses the event semantics and directly accesses its implementation (typically the underlying field of the event). The C# compiler is known to bypass the semantics when the event is accessed directly from the class that declares it. In this case, it directly accesses the private field whenever it can. For the same reason, OnAddHandler(EventInterceptionArgs) is not invoked for the initializer of a field-like event; add the initial handler in the constructor instead.

note

All classes implementing IAspect should typically be marked as serializable using the SerializableAttribute or PSerializableAttribute custom attribute. Fields that are only used at runtime (and unknown at compile-time) should be carefully marked with the NonSerializedAttribute or PNonSerializedAttribute custom attribute. When another aspect serializer (such as MsilAspectSerializer) is used, it is not necessary to mark the aspect class as serializable. For more information, see Understanding Aspect Serialization.

Understanding Aspect Serialization

Constructors

Name Description
EventInterceptionAspect()

Methods

Name Description
CreateAspectConfiguration()

Method invoked at build time to create a concrete AspectConfiguration instance specifically for the current Aspect type.

OnAddHandler(EventInterceptionArgs)

Method invoked instead of the Add semantic of the event to which the current aspect is applied, i.e. when a new delegate is added to this event.

OnInvokeHandler(EventInterceptionArgs)

Method invoked when the event to which the current aspect is applied is fired, for each delegate of this event, and instead of invoking this delegate.

OnRemoveHandler(EventInterceptionArgs)

Method invoked instead of the Remove semantic of the event to which the current aspect is applied, i.e. when a delegate is removed from this event.

See Also