Overview

The Code Weaver component (implemented in the PostSharp.CodeWeaver namespace) is a facility that allows to transform the body of existing methods. This component is pluggable and low-level:

Model

The Code Weaver component follows only loosely the canonical concepts of Aspect-Oriented Programming (AOP).

The Code Weaver works is based on the principle that user code may inject instructions at locations in existing method bodies. The locations in code are called join points and the classes that emit instructions advices. The role of the code weaver component is to locate the join points, restructure the method body so that new instruction blocks are inserted at join points, and let the advices emit instructions in these instruction blocks.

Join Points

Join points are locations in the method body. The kinds of supported locations is predefined and non extensible. The following kinds of join points are currently supported:

Join points are represented by the JoinPoint class and kinds of join points by the JoinPointKinds enumeration.

Advices

Advices are classes that inject instructions at join points. Advices implement the IAdvice interface. This interface has the following semantics:

The Weaver

The Weaver is the object that really weaves advices into existing code. You interact with the weaver in two ways: first you add advices using the AddMethodLevelAdvice and AddTypeLevelAdvice methods, then, when all advices have been added, you call the Weave method to transform the whole module.

Integration in the Platform Infrastructure

Unless you have good reasons not to use the Platform Infrastructure, you normally won't create the Weaver object yourself. What you will do instead is to insert a custom task in the project. This task will require the CodeWeaver built-in task and will implement the IAdviceProvider interface. The Code Weaver will call your code so that you can, in the ProvideAdvice method, add your own advices.

This architecture ensures that aspects are perfectly pluggable.