Metalama 2027.0 adds interceptors to redirect method calls at build time
Metalama 2027.0 introduces call-site interceptors that rewrite selected method calls at build time, so a method from a NuGet package or the .NET libraries can be wrapped without touching its source. The post walks through a retry example built from a T# template, a hand-written audit redirect, and the scoping rules. The feature is in preview from 2027.0.4-preview.
Key points
- Interceptors rewrite call sites at build time, so the called method can live in a NuGet package or the .NET libraries without source access.
- A T# template using meta.Proceed() generates one interceptor per intercepted signature, and it covers async and generic methods.
- Hand-written interceptors are plain static methods that can be unit-tested; mark them [ExcludeInterceptors] to avoid infinite recursion.
- Scope follows the registration point: an aspect targets its own type, a namespace fabric its namespace, a project fabric the whole project or a query.
- Use WithArgs to pass constants or per-call values, such as a retry count, into the interceptor.
- The Metalama.Extensions.Interceptors package is available from 2027.0.4-preview; it is a preview and not supported for production.
Metalama 2027.0 adds call-site interceptors, a build-time feature that rewrites selected method calls to target an interceptor. Unlike an override aspect, which modifies the method and needs its source, an interceptor works on the callers, so the called method can come from the .NET libraries or a NuGet package. Interceptors are either plain static C# methods or generated from a T# template, one per signature. Their scope follows where they are registered: an aspect, a namespace or a project fabric.
The retry example wraps a vendor client's Get-prefixed methods so TimeoutException triggers up to three attempts, leaving non-idempotent calls alone. The package Metalama.Extensions.Interceptors arrives in 2027.0.4-preview, a preview not supported for production.
Real-world use
- Retrying transient timeouts from a vendor SDK client across a whole service layer without editing its callers.
- Auditing file deletions or external API calls in one project, recording the caller name and line automatically.
- Adding test seams to legacy code by redirecting Thread.Sleep or file system calls during tests.
Why it matters
You can add cross-cutting behavior such as retries or tracing to calls you do not own, without changing the callers or the library. Because it is still a preview, use it for experiments for now.
Related stories
- Writing executable specifications with Bobcat in C#Jeremy D. Miller (The Shade Tree Developer)5 d ago
- Bobcat 1.0 is out: readable C# specifications projected from your testsJeremy D. Miller (The Shade Tree Developer)6 d ago
- C# 14 extension members: the shadowing bug the compiler won't warn you aboutDEV Community: #csharp1 d ago