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:
- Pluggable because the transformations are not implemented in the Code Weaver itself, but in separate classes. Many transformations may be applied to the same method. The Code Weaver calls the proper transformation class in the mean time.
- Low-level because transformations actually inject directly MSIL instructions. It allows system developers to produce performant code but is unusable by application developers. This low-level Code Weaver may serve as a base to build high-level code weavers like PostSharp Laos.
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:
- Before, after or instead of a method call instruction.
- Before, after or instead of a field get/set and array element instruction.
- Before a throw instruction.
- Before the method body (i.e. method entry).
- After the method body (either always either only on exception),
- After instance initialization in the constructor.
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
RequiresWeavemethod determines whether the current advice applies to a given concrete join points. - And, most important, the
Weavemethod injects instructions in instruction blocks created by the code weaver at join points.
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.