Simulacra 0.18.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Simulacra --version 0.18.0
                    
NuGet\Install-Package Simulacra -Version 0.18.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Simulacra" Version="0.18.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Simulacra" Version="0.18.0" />
                    
Directory.Packages.props
<PackageReference Include="Simulacra" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Simulacra --version 0.18.0
                    
#r "nuget: Simulacra, 0.18.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Simulacra@0.18.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Simulacra&version=0.18.0
                    
Install as a Cake Addin
#tool nuget:?package=Simulacra&version=0.18.0
                    
Install as a Cake Tool

Simulacra

Source-generated proxies for .NET interfaces, delegates and classes. Every simulacrum is generated at compile time by a Roslyn incremental generator; nothing is emitted at runtime, so there is no Reflection.Emit or Castle fallback.

Usage

var calculator = Simulacrum.Create<ICalculator>(new DefaultValueBehavior());

calculator.AddBehavior((invocation, next) =>
    invocation.Method.Name == nameof(ICalculator.Add)
        ? invocation.CreateValueReturn(42)
        : next(invocation));

A library built on Simulacra exposes its own factory by marking a generic method with [SimulacrumGenerator]. The generator emits a simulacrum for every set of interfaces the method is called with:

public static class Stub
{
    [SimulacrumGenerator]
    public static T Of<T>() where T : class => Simulacrum.Create<T>(new DefaultValueBehavior());
}

var calculator = Stub.Of<ICalculator>();   // ICalculatorSimulacrum is generated for this call

How it works

  • The generator looks for calls to [SimulacrumGenerator] methods whose type arguments are concrete interfaces, and emits one internal sealed class per distinct interface set into Simulacra.Generated.
  • Every interface member is implemented explicitly, so members with the same signature on different interfaces never clash. Members with a default implementation keep it.
  • A single class that can be derived from works too: the simulacrum derives from it and overrides its public virtual, abstract and non-sealed override members, and its abstract members of any accessibility; everything else, Equals, GetHashCode and ToString included, keeps the class's code. A class is always created with a constructor named explicitly, its parameterless one included: Simulacrum.Create<T>(SimulacrumConstructor, behaviors) runs the constructor the token names by documentation comment ID, with its arguments. A factory that passes a constructor on is marked [SimulacrumGenerator(ConstructorArguments = true)]; any other is a compile error for a class. The pipeline is handed over through a thread-static before the base constructor runs, so the virtual calls a constructor makes go through it.
  • A behavior can run a class member's own implementation with invocation.CallOriginal(), when invocation.CanCallOriginal is true: the generator writes a base call for each override, so it allocates nothing. Abstract members, generic methods and members with ref or out parameters have none, nor do interface members.
  • With the Simulacra.Isolation package referenced (directly, or through Doppelganger.Isolation), a class simulacrum also routes the members it can't override into its pipeline: non-virtual and sealed instance members, through a profiler that attaches itself at startup and rewrites them when the simulacrum is first created. A sealed class can then be simulated too: its factory creates the class itself and registers the instance with its pipeline. Both generators look for the Simulacra.Isolation.SimulacrumHooks type to tell whether it is referenced.
  • A single delegate type works too: Simulacrum.Create<Func<int, string>>(...) returns a delegate whose calls go through the pipeline as calls of the delegate type's Invoke method, which the generated class implements publicly.
  • Each call becomes an IMethodInvocation that runs through the simulacrum's BehaviorPipeline. A behavior either returns a result or calls next. If no behavior handles a call, it throws NotImplementedException.
  • out arguments carry the caller's current value into the invocation; they are not reset first. A behavior that returns without setting them leaves the caller's variable as it was. DefaultValueBehavior sets them to default.
  • The interface MethodInfos are looked up once per simulacrum type in static fields. Properties and events are reported as their accessor methods, and generic methods are closed over the call's type arguments.
  • A module initializer registers each simulacrum with Simulacrum, so Simulacrum.Create finds it by interface set without Activator or assembly scanning.
  • The generated code is written by Scriban templates in src/Simulacra.Generator/Templates. Scriban is compiled into the generator from source, with PolySharp supplying the attributes netstandard2.0 lacks, so the package ships a single generator assembly.

Not supported (yet)

Reported as SIMULACRA001 at the call site: ref returns, ref struct parameters or returns (such as Span<T>), pointers, and static abstract members, and an abstract class member that isn't accessible from the calling assembly. Only interfaces, a single delegate type and a single class can be simulated: a sealed or static class, struct, record, or a delegate type or class together with other types is reported as SIMULACRA003 at the call site, rather than failing when the simulacrum is created. An interface that isn't accessible from the calling assembly is reported as SIMULACRA002. A class without a constructor a derived class can call is SIMULACRA005, and a class created by a method that passes no constructor, SIMULACRA006.

A generic factory that passes its type parameter on to a [SimulacrumGenerator] method must be marked [SimulacrumGenerator] itself: the simulacrum is generated, and the type checked, where the type parameter gets a concrete type, which the generator only looks at for marked methods. An unmarked one is reported as SIMULACRA004 at the inner call, as is a type parameter of a generic type, which no attribute can make concrete.

Layout

Project
src/Simulacra Runtime: Simulacrum, ISimulacrum, behaviors, invocation and pipeline. Packs the generator as an analyzer.
src/Simulacra.Generator The incremental source generator (netstandard2.0).
tests/Simulacra.Tests End-to-end tests, with the generator wired up as a consumer would get it.
tests/Simulacra.Generator.Tests Runs the generator through a GeneratorDriver and checks output and diagnostics.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Simulacra:

Package Downloads
Doppelganger

Doppelganger - an opinionated, source-generated mocking library for .NET, built on Simulacra

Simulacra.Isolation

Simulacra.Isolation - simulate non-virtual members, sealed classes and statics with Simulacra, through a profiler that attaches itself to the test process

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.23.0 0 11/24/2026
0.22.0 75 11/23/2026
0.21.0 73 11/23/2026
0.19.0 148 11/10/2026
0.18.0 128 11/9/2026
0.17.0 132 11/8/2026
0.16.0 119 11/8/2026
0.15.0 118 11/7/2026
0.14.0 118 11/7/2026
0.13.0 126 11/7/2026
0.12.0 122 11/4/2026
0.11.0 129 11/1/2026
0.10.0 124 11/1/2026
0.0.9 128 11/1/2026
0.0.8 114 10/31/2026
0.0.7 117 10/31/2026
0.0.6 112 10/31/2026
0.0.5 120 10/31/2026
0.0.4 119 10/31/2026
0.0.3 113 10/31/2026
Loading failed