Table of Contents

Class Extensions

Namespace
Albatross.CodeAnalysis
Assembly
Albatross.CodeAnalysis.dll

Provides extension methods for working with Roslyn type symbols, including helpers for nullable types, collections, generic types, and symbol comparison.

public static class Extensions
Inheritance
Extensions
Inherited Members

Methods

GetDistinctProperties(INamedTypeSymbol, bool)

Gets distinct public properties from a type symbol, optionally including base class properties. If a property is overridden, only the most derived version is included.

public static IEnumerable<IPropertySymbol> GetDistinctProperties(this INamedTypeSymbol symbol, bool useBaseClassProperties)

Parameters

symbol INamedTypeSymbol

The named type symbol.

useBaseClassProperties bool

If true, includes properties from base classes; otherwise, only direct properties.

Returns

IEnumerable<IPropertySymbol>

An enumerable of distinct property symbols.

GetFullName(ITypeSymbol)

Gets the fully qualified name of a type symbol, including namespace and generic type arguments. For arrays, appends brackets to the element type name.

public static string GetFullName(this ITypeSymbol symbol)

Parameters

symbol ITypeSymbol

The type symbol.

Returns

string

The fully qualified type name.

GetFullNamespace(INamespaceSymbol)

Gets the fully qualified namespace name for a namespace symbol. Returns an empty string for the global namespace.

public static string GetFullNamespace(this INamespaceSymbol symbol)

Parameters

symbol INamespaceSymbol

The namespace symbol.

Returns

string

The fully qualified namespace name.

GetProperties(INamedTypeSymbol, bool)

Gets all public properties (with both public getter and setter) from a type symbol. Optionally includes properties from base classes.

public static IEnumerable<IPropertySymbol> GetProperties(this INamedTypeSymbol symbol, bool useBaseClassProperties)

Parameters

symbol INamedTypeSymbol

The named type symbol.

useBaseClassProperties bool

If true, includes properties from base classes; otherwise, only direct properties.

Returns

IEnumerable<IPropertySymbol>

An enumerable of property symbols.

GetRequiredSymbol(Compilation, string)

Gets a type symbol by its metadata name, throwing an exception if not found. Useful when a type is known to exist in the compilation.

public static INamedTypeSymbol GetRequiredSymbol(this Compilation compilation, string typeName)

Parameters

compilation Compilation

The compilation to search.

typeName string

The fully qualified metadata name of the type.

Returns

INamedTypeSymbol

The named type symbol.

Exceptions

ArgumentException

Thrown when the type is not found in the compilation.

GetTypeNameRelativeToNamespace(ITypeSymbol, string)

Gets the type name relative to a specified namespace. If the type is in the current namespace, returns only the type name without the namespace prefix; otherwise, returns the fully qualified name.

public static string GetTypeNameRelativeToNamespace(this ITypeSymbol symbol, string currentNamespace)

Parameters

symbol ITypeSymbol

The type symbol.

currentNamespace string

The current namespace for reference.

Returns

string

The type name, relative to the current namespace if applicable.

HasInterface(ITypeSymbol, INamedTypeSymbol?)

Determines whether a type symbol implements a specified interface (directly or indirectly).

public static bool HasInterface(this ITypeSymbol typeSymbol, INamedTypeSymbol? interfaceSymbol)

Parameters

typeSymbol ITypeSymbol

The type symbol to check.

interfaceSymbol INamedTypeSymbol

The interface to check for.

Returns

bool

True if the type implements the interface; otherwise, false.

Is(ISymbol?, ISymbol?)

Compares two symbols for equality using the default symbol equality comparer.

public static bool Is(this ISymbol? left, ISymbol? right)

Parameters

left ISymbol

The first symbol to compare.

right ISymbol

The second symbol to compare.

Returns

bool

True if the symbols are equal; otherwise, false.

IsCollection(ITypeSymbol?, Compilation)

Determines whether a type symbol represents a collection type (implements IEnumerable or is an array). Excludes System.String even though it implements IEnumerable.

public static bool IsCollection(this ITypeSymbol? symbol, Compilation compilation)

Parameters

symbol ITypeSymbol

The type symbol to check.

compilation Compilation

The compilation context.

Returns

bool

True if the type is a collection; otherwise, false.

IsConcreteClass(INamedTypeSymbol?)

Determines whether a named type symbol is a concrete class (non-abstract, non-static, non-generic-definition class).

public static bool IsConcreteClass(this INamedTypeSymbol? symbol)

Parameters

symbol INamedTypeSymbol

The named type symbol to check.

Returns

bool

True if the type is a concrete class; otherwise, false.

IsConstructedFromDefinition(INamedTypeSymbol?, INamedTypeSymbol)

Determines whether a named type symbol is constructed from a specific generic type definition.

public static bool IsConstructedFromDefinition(this INamedTypeSymbol? typeSymbol, INamedTypeSymbol genericDefinitionSymbol)

Parameters

typeSymbol INamedTypeSymbol

The type symbol to check.

genericDefinitionSymbol INamedTypeSymbol

The generic type definition to compare against.

Returns

bool

True if the type is constructed from the specified definition; otherwise, false.

IsDerivedFrom(ITypeSymbol, INamedTypeSymbol?)

Determines whether a type symbol derives from a specified base type (directly or indirectly). Only works for class types.

public static bool IsDerivedFrom(this ITypeSymbol typeSymbol, INamedTypeSymbol? baseTypeSymbol)

Parameters

typeSymbol ITypeSymbol

The type symbol to check.

baseTypeSymbol INamedTypeSymbol

The base type to check for in the inheritance hierarchy.

Returns

bool

True if the type derives from the base type; otherwise, false.

IsGenericTypeDefinition(INamedTypeSymbol?)

Determines whether a named type symbol is a generic type definition (unbound generic type).

public static bool IsGenericTypeDefinition(this INamedTypeSymbol? symbol)

Parameters

symbol INamedTypeSymbol

The named type symbol to check.

Returns

bool

True if the type is a generic type definition; otherwise, false.

IsNullable(ITypeSymbol?, Compilation)

Determines whether a type symbol represents a nullable type (either a nullable reference type or nullable value type).

public static bool IsNullable(this ITypeSymbol? symbol, Compilation compilation)

Parameters

symbol ITypeSymbol

The type symbol to check.

compilation Compilation

The compilation context.

Returns

bool

True if the type is nullable; otherwise, false.

IsNullableReferenceType(ITypeSymbol?)

Determines whether a type symbol represents a nullable reference type (annotated with nullable annotation).

public static bool IsNullableReferenceType(this ITypeSymbol? symbol)

Parameters

symbol ITypeSymbol

The type symbol to check.

Returns

bool

True if the type is a nullable reference type; otherwise, false.

IsNullableValueType(ITypeSymbol?, Compilation)

Determines whether a type symbol represents a nullable value type (e.g., int?, DateTime?).

public static bool IsNullableValueType(this ITypeSymbol? symbol, Compilation compilation)

Parameters

symbol ITypeSymbol

The type symbol to check.

compilation Compilation

The compilation context.

Returns

bool

True if the type is a nullable value type; otherwise, false.

IsNumeric(ITypeSymbol?)

Determines whether a type symbol represents a numeric type (byte, short, int, long, float, double, decimal, etc.).

public static bool IsNumeric(this ITypeSymbol? symbol)

Parameters

symbol ITypeSymbol

The type symbol to check.

Returns

bool

True if the type is numeric; otherwise, false.

IsPartial(INamedTypeSymbol?)

Determines whether a named type symbol is declared as partial. Checks for the partial modifier in interface declarations.

public static bool IsPartial(this INamedTypeSymbol? symbol)

Parameters

symbol INamedTypeSymbol

The named type symbol to check.

Returns

bool

True if the type is partial; otherwise, false.

TryGetCollectionElementType(ITypeSymbol?, Compilation, out ITypeSymbol?)

Attempts to determine the element type of a collection type. Works with arrays and generic IEnumerable implementations. Excludes System.String.

public static bool TryGetCollectionElementType(this ITypeSymbol? typeSymbol, Compilation compilation, out ITypeSymbol? elementType)

Parameters

typeSymbol ITypeSymbol

The type symbol to check.

compilation Compilation

The compilation context.

elementType ITypeSymbol

When this method returns true, contains the element type; otherwise, null.

Returns

bool

True if the element type was determined; otherwise, false.

TryGetGenericTypeArguments(ITypeSymbol, INamedTypeSymbol, out ITypeSymbol[])

Attempts to extract the type arguments from a generic type that matches the specified generic definition.

public static bool TryGetGenericTypeArguments(this ITypeSymbol symbol, INamedTypeSymbol genericDefinition, out ITypeSymbol[] arguments)

Parameters

symbol ITypeSymbol

The type symbol to check.

genericDefinition INamedTypeSymbol

The generic type definition to match against.

arguments ITypeSymbol[]

When this method returns true, contains the type arguments; otherwise, an empty array.

Returns

bool

True if the type is a constructed generic type matching the definition; otherwise, false.

TryGetNullableValueType(ITypeSymbol?, Compilation, out ITypeSymbol?)

Attempts to extract the underlying value type from a nullable value type.

public static bool TryGetNullableValueType(this ITypeSymbol? symbol, Compilation compilation, out ITypeSymbol? valueType)

Parameters

symbol ITypeSymbol

The type symbol to check.

compilation Compilation

The compilation context.

valueType ITypeSymbol

When this method returns true, contains the underlying value type; otherwise, null.

Returns

bool

True if the type is a nullable value type; otherwise, false.