Open sandboxFocus

Class InstanceLevelAspect

Base class for aspects applied on types, but having the same lifetime as instances of the type to which they are applied.

Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
Syntax
[HasInheritedAttribute]
[MulticastAttributeUsage(MulticastTargets.Class, AllowExternalAssemblies = false, TargetTypeAttributes = MulticastAttributes.Instance)]
[AttributeUsage(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Interface, AllowMultiple = true)]
[Serializer(null)]
public abstract class InstanceLevelAspect : TypeLevelAspect, ITypeLevelAspect, ITypeLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, ICloneAwareAspect, IInstanceScopedAspect, IAspect
Remarks

This class is provided for convenience. You get the same functionality by deriving your aspect class directly from TypeLevelAspect and implementing the interface IInstanceScopedAspect.


See IInstanceScopedAspect for a discussion of instance-scoped aspects.

This class does not provide any advice. It is typically the base class of composite aspects that introduce interfaces or members into the target class, or that add advices to its members. Each instance of the target class gets its own copy of the aspect, created by CreateInstance(AdviceArgs) as a shallow copy (MemberwiseClone()) of the aspect instance, and Instance gives the target object.

An aspect derived from this class can declare the same advices as a TypeLevelAspect, with a pointcut for the advices that target members, and also IntroduceInterfaceAttribute and ImportMemberAttribute, which require an instance-scoped aspect. An instance advice method cannot apply to a static member of the target type.


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

Properties

Name Description
Instance

Gets the object to which the current aspect has been applied.

Methods

Name Description
CreateAspectConfiguration()

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

CreateInstance(AdviceArgs)

Creates a new instance of the aspect based on the current instance, serving as a prototype.

OnCloned(ICloneAwareAspect)

Method called after an object enhanced by the current aspect has been cloned using MemberwiseClone(). The this parameter refers to the new aspect instance in the cloned object.

RuntimeInitializeInstance()

Initializes the aspect instance. This method is invoked when all system elements of the aspect (such as member imports) have completed.

See Also