Open sandboxFocus

Class ImmutableAttribute

An aspect that marks the target type as immutable and reports warnings when fields are not read-only or when automatic properties have setters.

Inheritance
ImmutableAttribute
Namespace: Metalama.Patterns.Immutability
Assembly: Metalama.Patterns.Immutability.dll
Syntax
[Inheritable]
public class ImmutableAttribute : TypeAspect, IAspect<INamedType>, IAspect, ICompileTimeSerializable, ITemplateProvider, IEligible<INamedType>, IHierarchicalOptionsProvider
Remarks

This aspect serves three purposes:

  • It exposes the immutability of a type to other aspects, such as ObservableAttribute from Metalama.Patterns.Observability, to facilitate code analysis.
  • It represents the design intent visibly, enhancing code readability by eliminating the need for readers to infer immutability from the type implementation.
  • It reports warnings if a type marked as immutable contains mutable fields or auto-implemented properties with setters.

By default, the aspect represents shallow immutability, meaning all instance fields must be read-only and no automatic property may have a setter. To represent deep immutability, supply Deep to the constructor. Deep immutability requires, recursively, that all instance fields and automatic properties are of a deeply immutable type.

This aspect is automatically inherited by derived types due to the InheritableAttribute.

Constructors

Name Description
ImmutableAttribute(ImmutabilityKind)

Initializes a new instance of the ImmutableAttribute class.

Methods

Name Description
BuildAspect(IAspectBuilder<INamedType>)

Defines the aspect implementation by adding advice, child aspects, and validators to the target declaration.

Extension Methods

See Also