Open sandboxFocus

Method GetTarget

GetTarget<T>(IRef<T>, ICompilation, IGenericContext?)

Resolves a reference to its target declaration within a specific compilation.

Declaration
public static T GetTarget<T>(this IRef<T> reference, ICompilation compilation, IGenericContext? genericContext = null) where T : class, ICompilationElement
Parameters
Type Name Description
IRef<T> reference

The reference to resolve.

ICompilation compilation

The compilation in which to resolve the reference. This may be a different compilation version than the one from which the reference was obtained.

IGenericContext genericContext

The optional generic context for resolving references within generic types or methods. Pass this when the declaration needs to be resolved in the context of a specific generic instantiation.

Returns
Type Description
T

The resolved declaration.

Type Parameters
Name Description
T

The type of compilation element, such as IMethod, IProperty, or INamedType.

Remarks

To resolve a reference using the current execution context (e.g., within a template or BuildAspect), use GetTarget<T>(IRef<T>) instead.

Exceptions
Type Condition
InvalidOperationException

Thrown when the reference cannot be resolved in the specified compilation.

GetTarget(IRef, ICompilation, IGenericContext?, Type?)

Resolves a non-generic reference to its target declaration within a specific compilation.

Declaration
public static ICompilationElement GetTarget(this IRef reference, ICompilation compilation, IGenericContext? genericContext = null, Type? interfaceType = null)
Parameters
Type Name Description
IRef reference

The reference to resolve.

ICompilation compilation

The compilation in which to resolve the reference.

IGenericContext genericContext

The optional generic context for resolving references within generic types or methods.

Type interfaceType

The optional interface type to return. When null, the default interface for the declaration type is used.

Returns
Type Description
ICompilationElement

The resolved declaration.

Exceptions
Type Condition
InvalidOperationException

Thrown when the reference cannot be resolved in the specified compilation.

GetTarget<T>(IRef<T>)

Resolves a reference to its target declaration using the current Metalama execution context.

Declaration
public static T GetTarget<T>(this IRef<T> reference) where T : class, ICompilationElement
Parameters
Type Name Description
IRef<T> reference

The reference to resolve.

Returns
Type Description
T

The resolved declaration.

Type Parameters
Name Description
T

The type of compilation element, such as IMethod, IProperty, or INamedType.

Remarks

This is the most common way to resolve a reference. It uses Current to obtain the current compilation. Use this method when resolving references during aspect execution.

For resolving references in a specific compilation (e.g., when comparing declarations across compilation versions), use GetTarget<T>(IRef<T>, ICompilation, IGenericContext?) instead.

Exceptions
Type Condition
InvalidOperationException

Thrown when the reference cannot be resolved or when called outside of a Metalama execution context (e.g., from a template, BuildAspect, or fabric).