Open sandboxFocus

Class OnMethodBoundaryAspect

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.

Inheritance
OnMethodBoundaryAspect
Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
Syntax
[AttributeUsage(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Event|AttributeTargets.Interface, AllowMultiple = true, Inherited = false)]
[MulticastAttributeUsage(MulticastTargets.Method|MulticastTargets.InstanceConstructor|MulticastTargets.StaticConstructor, AllowMultiple = true, TargetMemberAttributes = MulticastAttributes.NonAbstract)]
[HasInheritedAttribute]
[AspectConfigurationAttributeType(typeof(OnMethodBoundaryAspectConfigurationAttribute))]
[Serializer(null)]
public abstract class OnMethodBoundaryAspect : MethodLevelAspect, IMethodLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, IOnStateMachineBoundaryAspect, IOnMethodBoundaryAspect, IMethodLevelAspect, IAspect
Remarks

The OnMethodBoundaryAspect aspect results in the target method being wrapped in 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 succeeds (that is, when it 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)
{
OnEntry();
try
{
// Original method body.
OnSuccess();
return returnValue;
}
catch ( Exception e )
{
OnException();
}
finally
{
OnExit();
}
}

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

On async and iterator methods, the aspect is applied by default to the code of the method as written in the source, not to the compiler-generated code that creates the state machine (see SemanticallyAdvisedMethodKinds). Two more advices are then available: OnYield(MethodExecutionArgs), invoked when the method is suspended by an await or yield return statement, and OnResume(MethodExecutionArgs), invoked when it resumes.

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

  • Get or set arguments. Input and output arguments are available on the property Arguments. You can change output (out) and by-reference (ref) arguments, but not input arguments. If you need to modify arguments passed by value, consider using a MethodInterceptionAspect (see property Arguments).
  • Get the current exception. The current exception is available in the property Exception (only from the OnException(MethodExecutionArgs) advice). You can also replace the exception (see Exception for details).
  • Get or set the return value. The return value is available in the ReturnValue property. You can also modify it.
  • Change the method control flow. You can change the value of the FlowBehavior property to specify whether the target method should continue to execute after the execution of the current advice. This may be useful to implement a caching aspect or an exception handler.
  • Share state between advices. You can use the MethodExecutionTag property to store state between the execution of different advices related to the same execution of a method. For instance, you can store a cache key in OnEntry(MethodExecutionArgs) and retrieve it in OnSuccess(MethodExecutionArgs). Using MethodExecutionTag is the only way to share state that is both thread-safe and reentrant.

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.

note

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 another aspect serializer (such as MsilAspectSerializer) is used, it is not necessary to mark the aspect class as serializable. For more information, see Understanding Aspect Serialization.

Understanding Aspect Serialization

Constructors

Name Description
OnMethodBoundaryAspect()

Properties

Name Description
SemanticallyAdvisedMethodKinds

Gets or sets which target methods are advised semantically. This affects the behavior of the aspect when it is applied to iterator or async methods, which are compiled into state machines.

UnsupportedTargetAction

Gets or sets the action to take when the aspect is applied to an unsupported target method.

Methods

Name Description
CreateAspectConfiguration()

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

OnEntry(MethodExecutionArgs)

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

OnException(MethodExecutionArgs)

Method executed after the body of the methods to which this aspect is applied, when the method ended with an exception.

OnExit(MethodExecutionArgs)

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

OnResume(MethodExecutionArgs)

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

OnSuccess(MethodExecutionArgs)

Method executed after the body of the methods to which this aspect is applied, but only when the method returns successfully, that is, when no exception is thrown out of the method.

OnYield(MethodExecutionArgs)

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.

See Also