Advice effect is a code execution that can be injected into a method and, by extension, into properties and events as they use methods under the hood.
Advice is defined as a method within the aspect class and marked with an [Advice]
attribute. This attribute has the only required parameter that determines the kind of the advice.
Advice method can also accept various arguments. See Advice Effect Arguments to find out more.
class LogAspect {
[Advice(Kind.Before)]
public void LogEnter() {
Console.WriteLine("Entering ...");
}
[Advice(Kind.After)]
public void LogExit() {
Console.WriteLine("Leaving ...");
}
[Advice(Kind.Around)]
public object LogAndMeasureTimings(...) {
...
}
}
There are three kind of advice at the moment:
void
, the advice has to return at least null
. Special case for constructors - Around advice cannot be applied around constructors.Advice can target certain type of members via the second parameter of an [Advice]
attribute.
class LogAspect {
[Advice(Kind.Before, Targets = Target.Public | Target.Setter)]
public void LogEnter() {
Console.WriteLine("Entering public property setter...");
}
}
The Target
enumeration has the following options:
Complex values:
Access values:
Scope values:
Type values: