Open sandboxFocusImprove this doc

Developing Simple Aspects

Simple aspects are aspects that are composed of a single transformation. Developing a simple aspect in PostSharp is straightforward: you create a new class, derive it from a primitive aspect class, and override some special methods named advices.

If your aspect cannot be implemented as a single transformation, see Developing Member-Level Aspects for several behaviors on one member, or Developing Type- and Assembly-Level Aspects for behaviors on several members of a type or an assembly.

The methods of a simple aspect receive an object, such as MethodExecutionArgs, that is allocated on each call and gives untyped access to the arguments and the return value. To avoid this allocation and get typed values, write the aspect with advices and bound parameters instead (see Modernizing Your Custom Aspects).

In this section

Article Description
Injecting Behaviors Before and After Method Execution This article shows how to execute code when a method starts, succeeds, or fails, around await operators of async methods, or yield return statements of iterator methods.
Handling Exceptions This article describes how to write an aspect that handles exceptions.
Intercepting Methods This article explains how to intercept the execution of methods.
Intercepting Properties and Fields This article shows how to intercept read and write accesses to fields and properties.
Intercepting Events This article describes how to intercept the action of adding a delegate to an event, removing a delegate from an event, and invoking a delegate by raising the event.
Introducing Custom Attributes This article shows how to add a custom attribute to any element of code.
Introducing Managed Resources This article shows how to add a managed resource to the current assembly.