Skip to content

Symbol Extensions

Extensions for Roslyn symbol types.

See also: Extensions Overview


ISymbol Extensions

Available on all Roslyn symbols.

Symbol Equality

bool same = symbol.IsSymbol(otherSymbol);      // semantic equality
bool different = symbol.DoesNotEqual(otherSymbol);

Attributes

// Get all attributes as ValidAttribute projections
IEnumerable<ValidAttribute> attrs = symbol.GetAttributes();

// Get by name (handles both "Name" and "NameAttribute" forms)
IEnumerable<ValidAttribute> attrs = symbol.GetAttributesByName("Obsolete");

// Get by type
IEnumerable<ValidAttribute> attrs = symbol.GetAttributesByType<ObsoleteAttribute>();

Accessibility Checks

symbol.IsPublic()
symbol.IsPrivate()
symbol.IsProtected()
symbol.IsInternal()
symbol.IsProtectedInternal()
symbol.IsPrivateProtected()

Modifier Checks

symbol.IsVirtual()
symbol.IsOverride()
symbol.IsSealed()
symbol.IsAbstract()
symbol.IsStatic()
symbol.IsExtern()
symbol.IsObsolete()          // has [Obsolete] attribute
symbol.IsImplicitlyDeclared()

Source Location

symbol.IsFromSource()        // defined in source code
symbol.IsFromMetadata()      // from referenced assembly

ITypeSymbol Extensions

Start query builders and check type characteristics. For the full categorized reference, see Type Extensions.

Query Builders

MethodQuery methods = typeSymbol.QueryMethods();
PropertyQuery properties = typeSymbol.QueryProperties();
FieldQuery fields = typeSymbol.QueryFields();
ConstructorQuery constructors = typeSymbol.QueryConstructors();
EventQuery events = typeSymbol.QueryEvents();
ImmutableArray<AttributeData> attrs = typeSymbol.QueryAttributes();

Task Type Checks

typeSymbol.IsTaskType()              // Task or ValueTask (with or without T)
typeSymbol.IsValueTaskType()         // ValueTask or ValueTask<T>
typeSymbol.IsGenericTaskType()       // Task<T>
typeSymbol.IsGenericValueTaskType()  // ValueTask<T>
typeSymbol.IsNonGenericTaskType()    // Task (no type arg)
typeSymbol.IsNonGenericValueTaskType() // ValueTask (no type arg)

Collection Type Checks

typeSymbol.IsEnumerableType()        // IEnumerable<T>
typeSymbol.IsCollectionType()        // ICollection<T>
typeSymbol.IsListType()              // IList<T>
typeSymbol.IsDictionaryType()        // IDictionary<TKey, TValue>
typeSymbol.IsQueryableType()         // IQueryable<T>
typeSymbol.IsObservableType()        // IObservable<T>
typeSymbol.IsSetType()               // ISet<T>
typeSymbol.IsReadOnlyListType()      // IReadOnlyList<T>
typeSymbol.IsReadOnlyCollectionType()    // IReadOnlyCollection<T>
typeSymbol.IsReadOnlyDictionaryType()    // IReadOnlyDictionary<TKey, TValue>
typeSymbol.IsHashSetType()               // HashSet<T>
typeSymbol.IsImmutableArrayType()        // ImmutableArray<T>
typeSymbol.IsImmutableListType()         // ImmutableList<T>
typeSymbol.IsImmutableDictionaryType()   // ImmutableDictionary<TKey, TValue>
typeSymbol.IsImmutableHashSetType()      // ImmutableHashSet<T>

Special Type Checks

typeSymbol.IsNullableValueType()     // Nullable<T>
typeSymbol.IsFuncType()              // Func<...>
typeSymbol.IsActionType()            // Action or Action<...>
typeSymbol.IsLazyType()              // Lazy<T>
typeSymbol.IsTupleType()             // ValueTuple
typeSymbol.IsArrayType()             // T[]
typeSymbol.IsPointerType()           // T*
typeSymbol.IsExpressionType()        // Expression<T>
typeSymbol.IsSpanType()              // Span<T>
typeSymbol.IsReadOnlySpanType()      // ReadOnlySpan<T>
typeSymbol.IsMemoryType()            // Memory<T>
typeSymbol.IsReadOnlyMemoryType()    // ReadOnlyMemory<T>

Common Value Types

typeSymbol.IsTimeSpanType()          // System.TimeSpan
typeSymbol.IsDateTimeType()          // System.DateTime
typeSymbol.IsDateTimeOffsetType()    // System.DateTimeOffset
typeSymbol.IsGuidType()              // System.Guid
typeSymbol.IsUriType()               // System.Uri
typeSymbol.IsCancellationTokenType() // System.Threading.CancellationToken

Equality

typeSymbol.ImplementsIEquatable()    // checks IEquatable<T>, primitives, enums, Nullable<T>

Roslyn / Library Types

typeSymbol.IsValidSymbolType()       // Deepstaging.Roslyn.ValidSymbol<T>
typeSymbol.IsRoslynSymbolType()      // Microsoft.CodeAnalysis.ISymbol or implementors

Type Kind Checks

typeSymbol.IsDelegateType()
typeSymbol.IsEnumType()
typeSymbol.IsInterfaceType()
typeSymbol.IsRecordType()            // record class or record struct
typeSymbol.IsStructType()            // struct (excluding enums)
typeSymbol.IsClassType()
typeSymbol.IsAbstractType()
typeSymbol.IsSealedType()
typeSymbol.IsStaticType()

Inheritance

bool result = typeSymbol.ImplementsOrInheritsFrom(baseType);
bool isException = typeSymbol.IsOrInheritsFrom("Exception", "System");
bool inherits = typeSymbol.InheritsFrom("Controller", "Microsoft.AspNetCore.Mvc");
ITypeSymbol? baseType = typeSymbol.GetBaseTypeByName("DbContext");

Type Arguments

ITypeSymbol? elementType = typeSymbol.GetSingleTypeArgument();

INamedTypeSymbol Extensions

Additional extensions for named types.

Modifier Checks

namedType.IsPartial()
namedType.IsGeneric()
namedType.IsOpenGeneric()
namedType.IsNestedType()
namedType.IsGeneratedCode()
namedType.IsNotGeneratedCode()

Constructors

namedType.HasParameterlessConstructor()

Interface Checks

namedType.ImplementsInterface("IDisposable")

Attribute Checks

namedType.HasAttributes()
namedType.HasAttribute("Serializable")

Arity

int arity = namedType.GetArity();

IMethodSymbol Extensions

Extensions for method symbols.

Query Parameters

ParameterQuery params = methodSymbol.QueryParameters();

Async Analysis

methodSymbol.IsAsync()
methodSymbol.IsAsyncVoid()           // async void
methodSymbol.IsAsyncValue()          // async with return value
methodSymbol.GetAsyncKind()          // enum: None, Void, Task, ValueTask, GenericTask, etc.
ITypeSymbol? returnType = methodSymbol.GetAsyncReturnType(); // T from Task<T>

Method Classification

methodSymbol.IsExtensionMethod()
methodSymbol.IsGenericMethod()
methodSymbol.IsOperator()
methodSymbol.IsConstructor()
methodSymbol.IsDestructor()
methodSymbol.IsPropertyAccessor()
methodSymbol.IsEventAccessor()
methodSymbol.IsPartialMethod()

Return Type

methodSymbol.ReturnsVoid()

Parameters

methodSymbol.HasParameters()
methodSymbol.HasRefOrOutParameters()