Open sandboxFocus

Class LocationInterceptionAspect

Aspect that, when applied on a location (field or property), intercepts invocations of the Get (OnGetValue(LocationInterceptionArgs)) and Set (OnSetValue(LocationInterceptionArgs)) semantics.

Inheritance
LocationInterceptionAspect
Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
Syntax
[MulticastAttributeUsage(MulticastTargets.Field|MulticastTargets.Property, TargetMemberAttributes = MulticastAttributes.NonAbstract|MulticastAttributes.NonLiteral, AllowExternalAssemblies = false, AllowMultiple = true)]
[HasInheritedAttribute]
[AttributeUsage(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Interface, AllowMultiple = true)]
[AspectConfigurationAttributeType(typeof(LocationInterceptionAspectConfigurationAttribute))]
[Serializer(null)]
public abstract class LocationInterceptionAspect : LocationLevelAspect, ILocationLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, ILocationInterceptionAspect, IOnInstanceLocationInitializedAspect, ILocationLevelAspect, IAspect
Remarks

This aspect can be applied indifferently to fields and properties, called locations because they both have the semantics of a slot where a value can be stored and retrieved.

Applying the Aspect on Properties

Applying an aspect of type LocationInterceptionAspect to a property results in the accessors of this property to be replaced by a call to the method OnGetValue(LocationInterceptionArgs) or OnSetValue(LocationInterceptionArgs) of the current class. The original body of the accessor is moved into a new method, which can be called by the aspect by invoking one of the methods ProceedGetValue(), ProceedSetValue(), GetCurrentValue() or SetNewValue(object), or by using the Binding object.

Applying the Aspect on Fields

Applying this aspect on a field transforms the field into a property of the same name, scope and visibility as the original field. The original field is removed. Aspect code can get or set the value of the field by calling the methods ProceedGetValue(), ProceedSetValue(), GetCurrentValue() or SetNewValue(object), or by using the Binding object.

note

Remember to use PropertyInfo instead of FieldInfo at runtime to reflect the original field. For this reason, you cannot store a FieldInfo in the aspect. Store a LocationInfo instead; even if a LocationInfo represents a field at build time, it will represent the corresponding PropertyInfo at runtime.

note

When you apply an aspect derived from LocationInterceptionAspect to a value-type field, you should not pass the value of this field by reference (as an argument to out and ref parameters in C#). Indeed, PostSharp is unable to detect read and write accesses through references.

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 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

Constructors

Name Description
LocationInterceptionAspect()

Methods

Name Description
CreateAspectConfiguration()

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

OnGetValue(LocationInterceptionArgs)

Method invoked instead of the Get semantic of the field or property to which the current aspect is applied, i.e. when the value of this field or property is retrieved.

OnInstanceLocationInitialized(LocationInitializationArgs)

Method invoked after an initial value is set for a field or property to which the current aspect is applied. The method is invoked only for instance fields and instance auto-implemented properties, and it is invoked after their inline initialization. For static fields and properties, and for accesses from the constructor, OnSetValue is invoked instead.

OnSetValue(LocationInterceptionArgs)

Method invoked instead of the Set semantic of the field or property to which the current aspect is applied, i.e. when the value of this field or property is changed.

See Also