Open sandboxFocus

Interface IInstanceScopedAspect

Runtime semantics of aspects that have the same lifetime as instances of types to which they are applied (or instances of the declaring type of members to which they are applied).

Namespace: PostSharp.Aspects
Assembly: PostSharp.dll
Syntax
[HasInheritedAttribute]
public interface IInstanceScopedAspect : IAspect
Remarks

Aspects that do not implement this interface are statically scoped: there is one aspect instance per target declaration, stored in a static field, even when the aspect is applied to an instance member. Implement this interface when the aspect needs state per instance of the target type, for instance to import dependencies from the target object.

When an aspect is instance-scoped, a new instance of this aspect is created whenever an instance of the type to which it has been applied is created (concretely, before the call to the base constructor and to the constructor of this type).

New aspect instances are obtained using the prototype design pattern. As any other aspect, an instance-scoped aspect has a static instance initialized at build time, then serialized, deserialized at runtime, and initialized by RuntimeInitialize. This static instance then serves as a prototype. When a new instance of the target type is created, the prototype is cloned by the method CreateInstance(AdviceArgs) (typically implemented by a call to MemberwiseClone()). Then some system initializations (for example, member import) are performed on the aspect instance, and finally the RuntimeInitializeInstance() method is invoked.

When an aspect implementing the IInstanceScopedAspect interface is applied on a static method, field, property, or event, the aspect will be statically scoped and the interface will be ignored.

Methods

Name Description
CreateInstance(AdviceArgs)

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

RuntimeInitializeInstance()

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

See Also