PostSharp 2027.0 advice parameter binding removes boxing and allocations from aspects
PostSharp 2027.0 makes advice parameter binding public: aspect advices receive typed parameters instead of a MethodExecutionArgs object. That removes per-call heap allocations, boxing and casts on hot paths. Bindings are checked at build time, so mismatches fail the build rather than production.
Key points
- Derive from MethodLevelAspect and turn overrides into advice methods marked with attributes such as [OnMethodEntryAdvice].
- [State( StateScope.MethodInvocation )] replaces MethodExecutionTag with a typed variable, so no cast is needed.
- [ReturnValue] on a generic parameter T avoids boxing, and PostSharp infers T separately for each target method.
- [DeclarationName] is computed at build time, so no reflection call happens at run time.
- Binding mismatches are build errors such as LA0220 and LA0225, and all errors of an aspect are reported in one build.
- Event, field, parameter, OnInvokeAsync and yield-value aspects still need the AdviceArgs signature.
PostSharp 2027.0 makes advice parameter binding public, documented and checked at build time. Until now, an aspect built on OnMethodBoundaryAspect received a MethodExecutionArgs object on every call, which meant a heap allocation, boxed arguments and return values, and casts for the tag. In the new model, the aspect derives from MethodLevelAspect and each advice declares only the typed values it needs, such as the method name, a per-invocation State variable, the return value or the exception.
For hot-path code, this can cut most of the runtime overhead without rewriting the aspect logic. Event, field and parameter validation aspects, iterator yield values and OnInvokeAsync are not covered yet, so they can keep the older signature. Mismatched bindings surface as LA-coded build errors instead of runtime failures.
Real-world use
- A logging aspect on a high-throughput request handler that should not allocate on every call.
- A profiling aspect that times a service method and shares a Stopwatch between advices through [State].
- A generic retry or caching aspect that needs the exact return type of each target method.
Why it matters
Hot-path aspects can drop per-call allocations and boxing, and wrong bindings are caught as build errors before deployment. Event and async interception aspects should stay on the older model for now.