PostSharp 2027.0 makes advice parameter binding a public feature: advices receive typed parameters instead of an object allocated on each call. It also adds support for .NET 11 and C# 15, and replaces the Windows options application with a cross-platform browser interface and a command line tool.
Note
This release includes several breaking changes. Refer to Breaking Changes in PostSharp 2027.0 for more detailed information.
Advice parameter binding
Until now, the advices of an aspect received their context through an AdviceArgs object, such as MethodExecutionArgs. This object is allocated on the heap on each call, and it gives access to the arguments and the return value as object values, which the aspect must cast.
With advice parameter binding, an advice method declares the values it needs as typed parameters. A custom attribute on each parameter selects the value: an argument, the instance, the return value, the exception, a variable shared between advices, the reflection object of the target, and so on. The woven code computes only these values and allocates no object.
The following aspect measures the duration of a method:
[PSerializable]
public sealed class ProfileAttribute : MethodLevelAspect
{
[OnMethodEntryAdvice]
public void OnEntry( [State( StateScope.MethodInvocation )] out Stopwatch stopwatch )
{
stopwatch = Stopwatch.StartNew();
}
[OnMethodExitAdvice]
public void OnExit( [DeclarationName( IncludeTypeName = true )] string methodName,
[State( StateScope.MethodInvocation )] Stopwatch stopwatch )
{
Console.WriteLine( "{0} took {1} ms.", methodName, stopwatch.ElapsedMilliseconds );
}
}
The Pattern Libraries already used this mechanism internally. PostSharp 2027.0 makes it public, with the following improvements:
- PostSharp checks the bound parameters against each target at build time. An advice that does not fit a target causes an error with a message code, instead of a compiler crash or invalid code.
- New bindings give an advice the exception (
[Exception]), the return value in the method boundary advices ([ReturnValue]), and the reflection object of the target ([Declaration]). - Advice methods can be generic. PostSharp infers the type arguments for each target, so values of value types are not boxed.
[CurrentTask]gives the task that the caller of an async method observes, also for methods that returnValueTask.[SelfPointcut]and the Master property are optional when the grouping of the advices is unambiguous. An aspect that applies to a single method, field, property or event needs neither of them.
The documentation of composite aspects has been reorganized around this feature. See Developing Member-Level Aspects, Advice Parameters and Developing Type- and Assembly-Level Aspects. To convert an existing aspect, see Modernizing Your Custom Aspects.
Support for .NET 11 and C# 15
PostSharp 2027.0 supports the .NET 11 SDK and C# 15, MSBuild 18.9 and 18.10, and Visual Studio 18.9 and 18.10. The supported target frameworks move to .NET 10 and .NET 11, and the .NET 10 SDK is the minimum to build. See Breaking Changes in PostSharp 2027.0.
Runtime async
With <Features>runtime-async=on</Features>, the C# compiler implements async methods without a state machine, and the runtime suspends and resumes them. PostSharp 2027.0 applies aspects to these methods. The advices run at the same points and receive the same values as with a state machine. The only difference is that [CurrentTask] is null, because the runtime provides no task while the method runs.
Cross-platform options interface and command line tool
The Windows options application is replaced by a browser-based user interface and by the postsharp command line tool. Both are built on the infrastructure engine of Metalama, which is mature and cross-platform, so they work the same on Windows, Linux and macOS.
Install the tool as a .NET global tool:
dotnet tool install -g PostSharp.Tool
The tool registers license keys, edits the configuration of PostSharp, and reports the state of telemetry:
postsharp license register <key>
postsharp license list
postsharp ui
postsharp shutdown
postsharp ui opens the configuration pages in your browser. On Windows, the toast notifications that PostSharp shows after a build, for instance when a license is about to expire, open the same pages. postsharp shutdown stops the PostSharp processes that keep running after a build, such as the pipe server. See Deploying License Keys.
PostSharp 2027.0 also accepts license keys signed with the Elliptic Curve Digital Signature Algorithm (ECDSA), which can be verified on every platform, including macOS with .NET 11. The license keys issued before remain valid.
Telemetry
Usage reporting (telemetry) is now switched on at the first build, unless you opt out, as in Metalama. A notification informs you when this happens. Crash reports are still sent only after you agree.
To opt out on a machine, set the POSTSHARP_TELEMETRY_OPT_OUT environment variable. To opt out for all the builds of a repository, set the TelemetryEnabled property in the postsharp.config file at the root of the repository:
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<Property Name="TelemetryEnabled" Value="False" />
</Project>
Build integration
- PostSharp supports the MSBuild server, which the .NET 11 SDK enables by default.
TreatWarningsAsErrorsnow applies to the warnings of PostSharp and of aspects. See Ignoring and Escalating Warnings.- Add-ins compile against the reference assemblies of the new
PostSharp.Sdkpackage.
Plugin for AI coding agents
PostSharp 2027.0 provides a plugin for AI coding agents. The plugin supports Claude Code and OpenAI Codex. It contains this documentation, the API reference, and scripts that look up an article or an API. The agent can therefore check the real API before it writes PostSharp code. The plugin also instructs the agent to build the project to verify the code.
Install the plugin from the PostSharp.AI.Skills marketplace. For instructions, see Configuring AI Coding Agents.
Bug fixes
PostSharp 2027.0 includes many bug fixes in the compiler and in the Pattern Libraries. For the complete list, see the release notes of each build on GitHub.