Compile-time stunts support Native AOT on .NET 8 and later. Interfaces, classes, delegates, additional interfaces, constructor interception, default interface members and closed generic types are supported.
<PropertyGroup>
<PublishAot>true</PublishAot>
</PropertyGroup>
Stunts are created without reflection: the generator emits a module initializer that registers a
typed constructor for every stunt it generates. Those registrations also preserve the metadata the
pipeline reads (MethodBase, parameters) from trimming. DefaultValueBehavior gets typed
factories for the defaults the generated stunts need: arrays, enumerables, tasks, value tasks,
tuples and out values. Defaults for other types that only exist at run time need explicit
DefaultValueProvider.Register<T> factories.
Use Ref.Create<T> rather than the reflection-based Ref.Create(Type, object) overload, which
carries the standard .NET AOT and trimming warnings.
PublishAot, IsAotCompatible or EnableAotAnalyzer turn on the Stunts compatibility warnings
in ordinary builds, so you see them before publishing:
| Warning | Limitation | Alternative |
|---|---|---|
ST014 |
Run-time proxies require dynamic code | Enable compile-time stunts |
ST015 |
Generic intercepted methods use run-time MakeGenericMethod |
Use non-generic members with closed parameter and return types |
ST015 |
IQueryable defaults require dynamic code |
Register an AOT-compatible typed DefaultValueProvider factory, or avoid DefaultValueBehavior for that member |
Ref-struct signatures need .NET 9 and C# 13 or later (ST023), and ref-struct and pointer signatures need unsafe blocks (ST009).
A stunt type built from a run-time Type is registered with [assembly: Stunt<...>], as in
Custom factories. Asking for a stunt that was not
generated throws a NotSupportedException whose message has the exact attribute to add.
Stunts.Extensions is AOT compatible too: data annotations are checked by generated code rather than reflection.