Simulacra.Isolation
0.18.0
See the version list below for details.
dotnet add package Simulacra.Isolation --version 0.18.0
NuGet\Install-Package Simulacra.Isolation -Version 0.18.0
<PackageReference Include="Simulacra.Isolation" Version="0.18.0" />
<PackageVersion Include="Simulacra.Isolation" Version="0.18.0" />
<PackageReference Include="Simulacra.Isolation" />
paket add Simulacra.Isolation --version 0.18.0
#r "nuget: Simulacra.Isolation, 0.18.0"
#:package Simulacra.Isolation@0.18.0
#addin nuget:?package=Simulacra.Isolation&version=0.18.0
#tool nuget:?package=Simulacra.Isolation&version=0.18.0
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 oneinternal sealedclass per distinct interface set intoSimulacra.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,abstractand non-sealedoverridemembers, and its abstract members of any accessibility; everything else,Equals,GetHashCodeandToStringincluded, 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(), wheninvocation.CanCallOriginalis true: the generator writes a base call for each override, so it allocates nothing. Abstract members, generic methods and members withreforoutparameters have none, nor do interface members. - With the
Simulacra.Isolationpackage 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 theSimulacra.Isolation.SimulacrumHookstype 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'sInvokemethod, which the generated class implements publicly. - Each call becomes an
IMethodInvocationthat runs through the simulacrum'sBehaviorPipeline. A behavior either returns a result or callsnext. If no behavior handles a call, it throwsNotImplementedException. outarguments 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.DefaultValueBehaviorsets 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, soSimulacrum.Createfinds it by interface set withoutActivatoror 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 | Versions 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. |
-
net10.0
- Microsoft.Diagnostics.NETCore.Client (>= 0.2.750501)
- Simulacra (>= 0.18.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Simulacra.Isolation:
| Package | Downloads |
|---|---|
|
Doppelganger.Isolation
Doppelganger.Isolation - mock non-virtual members, sealed classes and statics with Doppelganger, through a profiler that attaches itself to the test process |
GitHub repositories
This package is not used by any popular GitHub repositories.