Open sandboxFocus

Class DisposableAttribute

Custom attribute that, when applied to a class, implements the IDisposable interface in such a way that the Dispose() method executes the Dispose() method for all child objects that also implement the IDisposable interface.

Namespace: PostSharp.Patterns.Model
Assembly: PostSharp.Patterns.Model.dll
Syntax
[IntroduceInterface(typeof(IDisposable), OverrideAction = InterfaceOverrideAction.Ignore)]
[MulticastAttributeUsage(Inheritance = MulticastInheritance.Strict)]
[Serializer(typeof(DisposableAttribute.Serializer))]
public sealed class DisposableAttribute : AggregatableClientAttribute, ITypeLevelAspect, ITypeLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, ICloneAwareAspect, IInstanceScopedAspect, IAspectProvider, IService, IAdviceProvider, IAspect, IDisposable
Remarks

The aspect relies on the AggregatableAttribute aspect to find children, so you must annotate the fields and properties that hold child objects with ChildAttribute. Children that do not implement IDisposable are ignored.

To add your own disposal logic, define a protected virtual void Dispose(bool disposing) method in the target class. The aspect calls it after it has disposed of the children.

The aspect does not introduce a finalizer. If you need one, implement it manually and call Dispose from it.

Constructors

Name Description
DisposableAttribute()
DisposableAttribute(PortableFormatterConstructorContext)

Properties

Name Description
ThrowObjectDisposedException

Gets or sets a value indicating whether an ObjectDisposedException should be thrown when an attempt is made to access an object that has already been disposed.

Methods

Name Description
Advise(object)

Injects the advice into the child object.

IsEligibleChild(object, ChildInfo)

Determines whether an object is eligible to be a child of the target instance of the current aspect.

OnAdviseChildFailed(object)

This method is invoked when a child is assigned to a parent and the advices of the parent cannot be added to the child. Implementations can choose to throw an exception (thread-aware attributes do this) or ignore the situation (DisposableAttribute does this).

See Also