Open sandboxFocus

Class MethodInterceptionAspect

Aspect that, when applied on a method, intercepts invocations of this method.

Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
Syntax
[MulticastAttributeUsage(MulticastTargets.Method|MulticastTargets.InstanceConstructor, AllowMultiple = true, AllowExternalAssemblies = true, PersistMetaData = false, TargetMemberAttributes = MulticastAttributes.NonAbstract)]
[HasInheritedAttribute]
[AttributeUsage(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Event|AttributeTargets.Interface, AllowMultiple = true)]
[AspectConfigurationAttributeType(typeof(MethodInterceptionAspectConfigurationAttribute))]
[Serializer(null)]
public abstract class MethodInterceptionAspect : MethodLevelAspect, IMethodLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, IAsyncMethodInterceptionAspect, IMethodInterceptionAspect, IMethodLevelAspect, IAspect
Remarks

Applying a MethodInterceptionAspect to a method results in the body of this method being replaced by a call to the aspect OnInvoke(MethodInterceptionArgs) method of the current class. The original method body is moved into a new method, which can be called by the aspect by invoking the Proceed() method, or by using the Binding object.

note

PostSharp creates a local copy of all by-reference parameters (in, out, ref). This local copy is passed to the intercepted method, not the original argument. This may have unwanted effects on multi-threaded code, because the argument value is not changed at the same moment as before. It may also have unwanted effects when the aspect is applied to a P-Invoke method, because a by-reference parameter could represent the starting address of a larger buffer.

When the target method returns a Task, OnInvoke(MethodInterceptionArgs) intercepts only the code that creates the task, not the asynchronous operation it represents. To intercept the whole operation, for example to handle its exceptions or its result, override OnInvokeAsync(MethodInterceptionArgs) and call ProceedAsync(). This is called semantic advising; see the SemanticallyAdvisedMethodKinds property.

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
MethodInterceptionAspect()

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 async method with an unsupported return value type.

Methods

Name Description
CreateAspectConfiguration()

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

OnInvoke(MethodInterceptionArgs)

Method invoked instead of the method to which the aspect has been applied.

OnInvokeAsync(MethodInterceptionArgs)

Method invoked instead of the method to which the aspect has been applied.

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