PostSharpAPI ReferencePost­Sharp.​AspectsMethod­Interception­Aspect
Open sandboxFocus

MethodInterceptionAspect Class

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

Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
public abstract class MethodInterceptionAspect : MethodLevelAspect, IMethodLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, IAsyncMethodInterceptionAspect, IMethodInterceptionAspect, IMethodLevelAspect, IAspect

Applying a MethodInterceptionAspect to a method results in the body of this method to be 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 object Binding.


PostSharp creates a local copy of all by-reference parameters (in, out, ref) and 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) or when applying the aspect on a P-Invoke method (because a by-reference parameter could represent the starting address of a larger buffer).


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


Name Description

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


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


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