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

Transforming Code: Concepts

Aspects can transform the target code by providing advice. An advice is a primitive transformation of code. Advice is safely composable: several aspects that do not know about each other can add advice to the same declaration.


The word advice, in English, is uncountable, i.e. grammatically plural. The grammatically-correct singular form of advice is piece of advice, but it seems very odd to use these words in a software engineering text. In aspect-oriented programming, advice is a countable concept. We sometimes use an advice for the singular form and advices for the plural form, which may be shocking to native English speakers. Whenever possible, we use neutral turns of phrases, unless it would make the phrase cumbersome or less understandable.

There are two ways to add advice: declaratively and imperatively.

Declarative advising

The only declarative advice is the member introduction advice which 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 is 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 some target code is advised, 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.