Skip to content

Roslyn semantic analysis

The compiler-grade symbol resolution Roslyn produces for C# code — type, binding, and reference information that AI agents can use as ground truth instead of guessing from text.

Roslyn is the open-source .NET compiler. Its semantic model answers questions tree-sitter cannot: what type is this expression, what does this identifier bind to, what calls this method? For an AI agent operating on a C# codebase, this is the difference between editing what the symbol looks like and editing what the symbol is.

Tree-sitter — the parser most agent tooling is built on — produces a syntax tree: it knows Cancel(orderId) is an invocation with one argument. It cannot tell you which Cancel — the instance method on OrderService, the extension method in Billing.Extensions, or nothing at all because the method was renamed last sprint. Syntax is what the code looks like; semantics is what it means after binding and overload resolution.

Roslyn answers the second question:

var model = await document.GetSemanticModelAsync();
var invocation = root.FindNode(span) as InvocationExpressionSyntax;
var info = model.GetSymbolInfo(invocation);

// info.Symbol == null      → the call target does not exist. Plan step rejected.
// info.Symbol is IMethodSymbol m → m.ContainingType, m.Parameters, m.DeclaringSyntaxReferences

GetSymbolInfo either resolves the reference to a real symbol — with its containing type, signature, and declaration location — or returns null plus candidate symbols. For an AI agent, the null is the valuable part: it converts “the model believes this method exists” into a checkable fact before any edit is attempted.

When it matters

  • Cross-file rename or extraction where text-based search misses overloads.
  • Verifying that an inferred call target actually exists before the agent writes a plan that depends on it.
  • Powering an MCP server that exposes type lookups to the agent so it does not have to re-derive them from grep.

Used in

Related terms