Open sandboxFocus

Method GetTargetOrNull

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

Resolves a reference to its target declaration within a specific compilation, returning null if the reference cannot be resolved.

Declaration
public static T? GetTargetOrNull<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.

Returns
Type Description
T

The resolved declaration, or null if the reference cannot be resolved (e.g., if the declaration was removed from the compilation).

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, use GetTargetOrNull<T>(IRef<T>) instead.

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

Resolves a non-generic reference to its target declaration within a specific compilation, returning null if the reference cannot be resolved.

Declaration
public static ICompilationElement? GetTargetOrNull(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, or null if the reference cannot be resolved.

GetTargetOrNull<T>(IRef<T>)

Resolves a reference to its target declaration using the current Metalama execution context, returning null if the reference cannot be resolved.

Declaration
public static T? GetTargetOrNull<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, or null if the reference cannot be resolved.

Type Parameters
Name Description
T

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

Remarks

This method uses Current to obtain the current compilation. It returns null instead of throwing when the reference cannot be resolved.