Metalama 1.0 / / Metalama Documentation / Conceptual Documentation / Creating Aspects / Transforming Code / Concepts

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.

Declarative advising

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 advising

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:

Template methods

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.