Custom attribute that, when applied to a class, applies the ImmutableThreadingModel to this class. The aspect introduces the IImmutable interface into the target class. Immutable objects cannot be modified after the constructor exits.
Inheritance
Implements
Namespace: PostSharp.Patterns.Threading
Assembly: PostSharp.Patterns.Threading.dll
Syntax
[IntroduceInterface(typeof(IImmutable), OverrideAction = InterfaceOverrideAction.Ignore, LinesOfCodeAvoided = 0)]
[Metric("UsedFeatures", "PostSharp.Patterns.Threading.Immutable")]
[Serializer(typeof(ImmutableAttribute.Serializer))]
public sealed class ImmutableAttribute : ReadOnlyThreadAwareAttribute, ITypeLevelAspect, ITypeLevelAspectBuildSemantics, IAspectBuildSemantics, IValidableAnnotation, ICloneAwareAspect, IInstanceScopedAspect, IAspectProvider, IService, IAdviceProvider, IAspect, IQueryInterface, IConcurrencyController, IImmutable, IThreadAwareRemarks
The object becomes read-only when the constructor of the most derived class has completed, so constructors of base classes and methods called by constructors can still set fields. Accessing the object from a different thread than the one that created it before the constructor has completed results in a ThreadMismatchException. Modifying the object after the constructor has completed results in an ObjectReadOnlyException.
The aspect implicitly adds the AggregatableAttribute aspect, so you can build immutable object trees. Children must be immutable or freezable; freezable children are frozen when the constructor of the parent completes. When the object must be modified after its constructor, use FreezableAttribute instead.
Constructors
| Name | Description |
|---|---|
| ImmutableAttribute() | |
| ImmutableAttribute(PortableFormatterConstructorContext) |
Methods
| Name | Description |
|---|---|
| OnInstanceConstructed() |