An aspect that, when applied to a get-only property or parameterless non-void method, caches the result and returns the cached value on subsequent calls.
Implements
Namespace: Metalama.Patterns.Memoization
Assembly: Metalama.Patterns.Memoization.dll
Syntax
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Property)]
public sealed class MemoizeAttribute : Attribute, IAspect<IMethod>, IEligible<IMethod>, IAspect<IProperty>, IAspect, ICompileTimeSerializable, ITemplateProvider, IEligible<IProperty>Remarks
The memoized result is stored in a field introduced by the aspect directly into the target type. The aspect uses CompareExchange<T>(ref T, T, T) to ensure thread-safe initialization without locking.
The underlying implementation of the property or method is not guaranteed to be executed only once. However, all invocations are guaranteed to return the same value or object reference.
For nullable reference types and value types, the cached value is wrapped in a StrongBox<T>, adding minor memory allocation overhead when the memoized member is evaluated. This design minimizes memory usage when few or no memoized properties or methods are evaluated.
Memoization serves as a simpler and higher-performance alternative to Lazy<T> for properties and parameterless methods that need to be computed only once.
Constructors
| Name | Description |
|---|---|
| MemoizeAttribute() |