PostSharp

Aspect Classes

The following table gives a list of available primitive aspect classes. Every aspect class is described in greater detailed in the class reference documentation.

Aspect Type

Targets

Description

OnMethodBoundaryAspect

Methods

Methods enhanced with an OnMethodBoundaryAspect are wrapped by a try/catch/finally construct. This aspect provides the advices OnEntry(MethodExecutionArgs), OnSuccess(MethodExecutionArgs), OnException(MethodExecutionArgs) and OnExit(MethodExecutionArgs); these advices are invoked directly from the transformed method. the return value, and the exception (if applicable). This aspect is useful to implement tracing or transaction handling, for instance.

OnExceptionAspect

Methods

Methods enhanced with an OnExceptionAspect are wrapped by a try/catch construct. This aspect provides the advice OnException(MethodExecutionArgs); this advice is invoked from the catch block. This aspect is useful to implement exception handling policies. Contrarily to OnMethodBoundaryAspect, this aspect lets you define the type of caught exceptions by overriding the method GetExceptionType(MethodBase)

MethodInterceptionAspect

Methods

When a method is enhanced by a MethodInterceptionAspect, all calls to this method are replaced by calls to OnInvoke(MethodInterceptionArgs), the only advice of this aspect type. This aspect is useful when the execution of target method can be deferred (asynchronous calls), must be dispatched on a different thread.

LocationInterceptionAspect

Fields, Properties

When a field or property is enhanced by a LocationInterceptionAspect, all calls to its accessors are replaced by calls to advices OnGetValue(LocationInterceptionArgs) and OnSetValue(LocationInterceptionArgs). Fields are transparently replaced by properties. This aspect is useful to implement functionalities that need to get or set the location value, such as the observability design pattern (INotifyPropertyChanged).

EventInterceptionAspect

Events

When an event is enhanced by an EventInterceptionAspect, all calls to its add and remove semantics are replaced by calls to advices OnAddHandler(EventInterceptionArgs) and OnRemoveHandler(EventInterceptionArgs). Additionally, when the event is fired, even of invoking directly the handlers that were added to the event, the advice OnInvokeHandler(EventInterceptionArgs) is called instead. This aspect is useful to add functionalities to events, such as implementing asynchronous events or materialized list of subscribers.

CompositionAspect

Types

This aspect introduces an interface into a type by composition. The interface is introduced statically; the aspect method GetPublicInterfaces(Type) should return the type of introduced interfaces. However, the object implementing the interface is created dynamically at runtime by the implementation of the method CreateImplementationObject(AdviceArgs).

CustomAttributeIntroductionAspect

Any

This aspect introduces a custom attribute on any element of code. A custom attribute can be represented as a CustomAttributeData or a ObjectConstruction.

ManagedResourceIntroductionAspect

Assemblies

This aspect introduces a managed resource into the current assembly.

Tip

The implementation of aspects OnMethodBoundaryAspect and OnExceptionAspect is very efficient; they should be preferred over other aspects whenever it makes sense.

posted @ 2012-02-07 15:25  awp110  阅读(191)  评论(0编辑  收藏  举报