Skip to content

Extensions

Convenience methods for common Roslyn operations.

See also: Queries | Projections | Emit

Overview

Extension methods organized by the type they extend. These wrap common Roslyn operations in fluent, null-safe APIs.

Category Description
Symbol Extensions ISymbol, INamedTypeSymbol, IMethodSymbol
Type Extensions ITypeSymbol — collections, async, classification, equality, and more
Compilation Extensions Compilation and namespace queries
Attribute Extensions AttributeData extraction
String Extensions Case conversion utilities
Domain Extensions Entity Framework, LanguageExt, JSON utilities

Usage Examples

Find Async Methods Returning Entity Types

var asyncMethods = typeSymbol.QueryMethods()
    .ThatArePublic()
    .ThatAreAsync()
    .Where(m => m.ReturnType.IsGenericTaskType())
    .GetAll();

Check If Type Is Repository

bool isRepo = typeSymbol.IsInterfaceType() &&
              typeSymbol.Name.EndsWith("Repository") &&
              ((INamedTypeSymbol)typeSymbol).ImplementsInterface("IRepository");

Extract Attribute Configuration

var config = symbol.GetAttributesByName("Configure")
    .FirstOrDefault()
    ?.Query()
    .WithArgs(attr => new Config
    {
        Name = attr.ConstructorArg<string>(0).OrDefault("Default"),
        Enabled = attr.NamedArg<bool>("Enabled").OrDefault(true),
        Priority = attr.NamedArg<int>("Priority").OrDefault(0)
    })
    .OrDefault(Config.Default);

Check Type Hierarchy

if (typeSymbol.InheritsFrom("ControllerBase", "Microsoft.AspNetCore.Mvc"))
{
    var actions = typeSymbol.QueryMethods()
        .ThatArePublic()
        .ThatAreInstance()
        .WithoutAttribute<NonActionAttribute>()
        .GetAll();
}

Analyze Async Method Signatures

var method = typeSymbol.QueryMethods()
    .WithName("ProcessAsync")
    .FirstOrDefault();

if (method.IsValid(out var valid))
{
    var asyncKind = valid.Value.GetAsyncKind();
    var innerType = valid.Value.GetAsyncReturnType();

    if (valid.Value.IsAsyncVoid())
    {
        // Report diagnostic: async void is problematic
    }
}

Filter by Generated Code

var userDefinedTypes = TypeQuery.From(compilation)
    .ThatAreClasses()
    .Where(t => t.IsNotGeneratedCode())
    .GetAll();