Transforming Code: Concepts
Aspects can transform the target code by providing advice. Advice are primitive transformations of code. Advice are safely composable: several aspects that do not know about each other can add advice to the same declaration.
There are two ways to add advice: declaratively and imperatively.
The only declarative advice is the member introduction advice and is marked by the IntroduceAttribute custom attribute. For each member of the aspect class annotated with
[Introduce], the aspect framework will attempt to introduce the member in the target class. For details, see Introducing Members.
Imperative advice are added by the implementation of the BuildAspect method thanks to the methods exposed by the Advice property of the
builder parameter. See IAdviceFactory for a complete list of methods. In short:
- Override allows you to replace the implementation of a type member.
- IntroduceMethod, IntroduceProperty, IntroduceField and IntroduceEvent allows your aspect to introduce new members into the target type. See Introducing Members for details.
- ImplementInterface makes the target type implement an interface. See Implementing Interfaces for details.
With most kinds of advice, you have to provide a template of the member that you want to add to the target type (whether a new member or a new implementation of an existing one).
Templates are made of standard C# code but mix two kinds of code: compile-time and run-time. When an advice is added to some target code, the compile-time part of the corresponding template is executed and what results is the run-time code, which is then added to the source code.
For details, see T# Template.