Open sandboxFocus

Method ToRef

ToRef()

Gets a reference that can be used to identify the current declaration across different compilation versions.

Declaration
IRef<IIndexer> ToRef()
Returns
Type Description
IRef<IIndexer>

A compile-time serializable reference to the current declaration.

Remarks

References are essential in Metalama because IDeclaration objects are bound to a specific ICompilation. As aspects execute, the compilation evolves through multiple immutable versions. References provide a stable way to track the same declaration across these versions.

Common use cases:

  • Storing declaration references in aspect fields for use in later compilation versions.
  • Serializing references for cross-project scenarios (inheritable aspects, validators).
  • Passing references to child aspects via IAspectState.

To resolve a reference back to the declaration, use GetTarget<T>(IRef<T>) or GetTarget<T>(IRef<T>, ICompilation, IGenericContext?).

See Also