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
symbolINamedTypeSymbolThe named type symbol.
useBaseClassPropertiesboolIf 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
symbolITypeSymbolThe 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
symbolINamespaceSymbolThe 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
symbolINamedTypeSymbolThe named type symbol.
useBaseClassPropertiesboolIf 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
compilationCompilationThe compilation to search.
typeNamestringThe 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
symbolITypeSymbolThe type symbol.
currentNamespacestringThe 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
typeSymbolITypeSymbolThe type symbol to check.
interfaceSymbolINamedTypeSymbolThe 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
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
symbolITypeSymbolThe type symbol to check.
compilationCompilationThe 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
symbolINamedTypeSymbolThe 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
typeSymbolINamedTypeSymbolThe type symbol to check.
genericDefinitionSymbolINamedTypeSymbolThe 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
typeSymbolITypeSymbolThe type symbol to check.
baseTypeSymbolINamedTypeSymbolThe 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
symbolINamedTypeSymbolThe 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
symbolITypeSymbolThe type symbol to check.
compilationCompilationThe 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
symbolITypeSymbolThe 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
symbolITypeSymbolThe type symbol to check.
compilationCompilationThe 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
symbolITypeSymbolThe 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
symbolINamedTypeSymbolThe 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
typeSymbolITypeSymbolThe type symbol to check.
compilationCompilationThe compilation context.
elementTypeITypeSymbolWhen 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
symbolITypeSymbolThe type symbol to check.
genericDefinitionINamedTypeSymbolThe generic type definition to match against.
argumentsITypeSymbol[]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
symbolITypeSymbolThe type symbol to check.
compilationCompilationThe compilation context.
valueTypeITypeSymbolWhen this method returns true, contains the underlying value type; otherwise, null.
Returns
- bool
True if the type is a nullable value type; otherwise, false.