Skip to content

Queries

Fluent builders for finding types, methods, properties, fields, constructors, events, and parameters.

See also: Projections | Emit | Extensions

Overview

Query builders let you compose chainable filters on Roslyn symbols:

  • Immutable — each method returns a new instance
  • Lazy — filters are applied when you call GetAll(), First(), etc.
  • Safe — materialization returns ValidSymbol<T> wrappers with guaranteed non-null access
// Find all public async methods returning Task<T>
var methods = typeSymbol.QueryMethods()
    .ThatArePublic()
    .ThatAreAsync()
    .ReturningGenericTask()
    .GetAll();

// Project results directly to your model
var models = TypeQuery.From(compilation)
    .ThatAreClasses()
    .WithAttribute("Entity")
    .Select(t => new EntityModel(t.Name, t.Namespace));

// Materialize to pipeline-safe snapshots
var snapshots = typeSymbol.QueryMethods()
    .ThatArePublic()
    .Snapshots();   // → EquatableArray<MethodSnapshot>

Query Types

Query Description
TypeQuery Find types in a compilation or namespace
MethodQuery Find methods on a type
PropertyQuery Find properties on a type
FieldQuery Find fields on a type
ConstructorQuery Find constructors on a type
EventQuery Find events on a type
ParameterQuery Find parameters on a method

Snapshot Terminals

All query builders support .Snapshots() and .Snapshot() terminals that materialize results into pipeline-safe snapshot types:

// All matching results as snapshots
EquatableArray<MethodSnapshot> methods = typeSymbol.QueryMethods()
    .ThatArePublic()
    .ThatAreInstance()
    .Snapshots();

// First matching result as snapshot (or null)
TypeSnapshot? entity = TypeQuery.From(compilation)
    .WithName("Customer")
    .Snapshot();

Use snapshot terminals when building pipeline models for incremental generators.


Custom Filters

All query builders support Where() for custom predicates:

var special = TypeQuery.From(compilation)
    .Where(t => t.GetMembers().Length > 10)
    .Where(t => t.ContainingNamespace?.Name == "Domain")
    .GetAll();

Common Patterns

Filter and Project

var models = TypeQuery.From(compilation)
    .ThatAreClasses()
    .WithAttribute("Entity")
    .Select(type => new EntityModel
    {
        Name = type.Name,
        Properties = type.Value.QueryProperties().ThatArePublic().GetAll()
    });

Early Exit with FirstOrDefault

var handler = typeSymbol.QueryMethods()
    .WithName("Handle")
    .WithParameterCount(1)
    .FirstOrDefault();

if (handler.IsNotValid(out var valid))
    return; // No handler found

// Use valid.Name, valid.Value, etc.

Chaining Queries

var targetMethods = new HashSet<string>(
    attribute.TargetType.QueryMethods()
        .ThatArePublic()
        .Select(m => m.Name)
);