PostSharpAPI ReferencePost­Sharp.​AspectsOn­Method­Boundary­Aspect
Open sandboxFocus

OnMethodBoundaryAspect Class

Aspect that, when applied to a method defined in the current assembly, inserts a piece of code before and after the body of these methods.

Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
public abstract class OnMethodBoundaryAspect : MethodLevelAspect, IMethodLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, IOnStateMachineBoundaryAspect, IOnMethodBoundaryAspect, IMethodLevelAspect, IAspect

The OnMethodBoundaryAspect aspect results in the target method to be wrapped into a try ... catch ... finally block. You can implement four advices: OnEntry(MethodExecutionArgs), executed at the beginning of the block; OnSuccess(MethodExecutionArgs), executed only when the method is successful (i.e. does not result in an exception); OnException(MethodExecutionArgs), invoked when the method results in an exception; and OnExit(MethodExecutionArgs), always executed after method execution (whether the method resulted in an exception or not).

Schematically, the aspect transforms the original method as follows:

int MyMethod(object arg0, int arg1)
    // Original method body. 
    return returnValue;
  catch ( Exception e )

Note that this code is only schematic; actually generated instructions are more complex because they have to cope with parameter boxing and control flow modification, among others.

An object of type MethodExecutionArgs is passed to every advice of this aspect. This object allows you to:

You can apply a method boundary aspect to a method that is outside your assembly. If you do, all calls to that method are intercepted and replaced with calls to a new method, in your assembly, that calls the original method. When this happens, by-reference parameters (ref) undergo special treatment similar to what happens in MethodInterceptionAspect.


All classes implementing IAspect should typically be marked as serializable using the SerializableAttribute or PSerializableAttribute custom attribute . Fields that are only used at runtime (and unknown at compile-time) should be carefully marked with the NonSerializedAttribute or PNonSerializedAttribute custom attribute. When PostSharp is used on a platform that does not support aspect serialization (such as .NET Compact Framework, Silverlight, or Windows Phone), or when another aspect serializer is used, it is not necessary to mark the aspect class as serializable. For more information, see Understanding Aspect Serialization .

Understanding Aspect Serialization


Name Description


Name Description

Determines which target methods will be advised semantically. This affects the behavior of the aspect when it's applied to iterator or async methods, which are compiled into state machines.


Specifies the action to take when the aspect is applied to an unsupported target method.


Name Description

Method invoked at build time to create a concrete AspectConfiguration instance specifically for the current Aspect type.


Method executed before the body of methods to which this aspect is applied.


Method executed after the body of methods to which this aspect is applied, in case that the method resulted with an exception.


Method executed after the body of methods to which this aspect is applied, even when the method exists with an exception (this method is invoked from the finally block).


Method executed when a state machine resumes execution after a yield return or await statement.


Method executed after the body of methods to which this aspect is applied, but only when the method successfully returns (i.e. when no exception flies out the method.).


Method executed when a state machine yields, as the result of a yield return or await statement.

SetAspectConfiguration(AspectConfiguration, MethodBase)

Method invoked at build time to set up an AspectConfiguration object according to the current Aspect instance and a specified target element of the current aspect.