Compile time and run time

By default, a Roslyn source generator writes the stunts at compile time. Nothing is generated while your program runs, which is what makes Stunts work where Reflection.Emit is forbidden or limited: physical iOS devices, game consoles and Native AOT.

Seeing the generated code

The generated stunts are regular C#. To keep them on disk, set:

<PropertyGroup>
  <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

They are written under obj/, in the folder of the Stunts generator.

What compile-time stunts support

Optional parameters, inherited generic constraints, long signatures, ref and out arguments, spans and other ref structs, default interface members, events, indexers, and nested types. Ref-struct and pointer signatures need <AllowUnsafeBlocks>true</AllowUnsafeBlocks> (ST009). Ref-struct signatures also need .NET 9 and C# 13 or later (ST023).

What can't be proxied is reported at the call or the [assembly: Stunt<T>] attribute that asks for it: see Diagnostics. Internal interface members are supported when the stunt is in the same assembly, or in a friend assembly through InternalsVisibleTo.

The generation cache

Generated stunts are cached in .vs/stunts-cache at the repository root, a folder the standard .NET .gitignore already excludes. A later build reuses a stunt as long as the types it implements keep the same declarations and member signatures, so changing method bodies, tests or unrelated types doesn't generate it again. Every project of the repository shares the cache, so an interface used by several test projects is generated once.

Property Effect
EnableCompileTimeStunts true by default on C#; false on Visual Basic or when Stunts.DynamicProxy is installed.
StuntsDynamicProxy Set to true by Stunts.DynamicProxy so run-time proxy projects do not trigger ST011.
StuntsCacheDir Another folder for the generation cache.
EnableStuntsCache false generates every stunt on every build.
EmitCompilerGeneratedFiles true writes generated .cs files to obj/ (pretty-printed).
SkipCompilerExecution true skips registering the compile-time generator and cache resolution for the build.
DebugStuntGenerator true launches the debugger when StuntGenerator executes (DebugSourceGenerators=true does the same for all source generators).

Set the STUNTS_PROFILE environment variable to a file path to record per-compilation generator timings and generation cache hit/miss counts.

Run-time stunts

Projects that can't or don't want to use compile-time stunts install Stunts.DynamicProxy, which creates the proxies at run time with Castle.Core:

<ItemGroup>
  <PackageReference Include="Stunts.DynamicProxy" Version="..." />
</ItemGroup>

The package sets EnableCompileTimeStunts to false and StuntsDynamicProxy to true. A project without compile-time stunts and without Stunts.DynamicProxy gets warning ST011.

Visual Basic only supports run-time stunts, so VB projects always need Stunts.DynamicProxy.

Assemblies using compile-time stunts and assemblies using Stunts.DynamicProxy can coexist in the same process: each assembly registers its own IStuntFactory via StuntFactory.Register, and StuntFactory.GetFactory resolves the factory per requesting assembly (with StuntFactory.Default and StuntFactory.UseAmbient available as process-wide and async-local overrides).

Proxies by hand

The pipeline doesn't need generated proxies: the ManualStunts package has the behavior pipeline alone, for proxies you write yourself.