| title | Home |
|---|---|
| layout | simple |
| og_type | website |
Interception for .NET
Stunts generates proxies at compile time and lets you configure them in code with a pipeline of behaviors. Nothing is emitted at run time, so it runs everywhere .NET does: Native AOT, iOS devices and game consoles included.
dotnet add package StuntsA stunt implements the interface, class or delegate you ask for. Every call goes through its behaviors, in order, and any of them can answer, change the arguments, call the next one or throw.
- Interface
- Class
- Delegate
- Builder
var stunt = Stunt.For<ICalculator>();
ICalculator calc = stunt.ToObject();
stunt.AddBehavior(
(call, next) => call.CreateValueReturn(call.Arguments.Get<int>("x") + call.Arguments.Get<int>("y")),
appliesTo: call => call.MethodBase.Name == nameof(ICalculator.Add));
calc.Add(2, 3); // 5public class Greeter
{
public virtual string Greet(string name) => $"Hello, {name}";
}
Greeter greeter = Stunt.For<Greeter>()
.AddBehavior((call, next) =>
{
call.Arguments.Set(0, "Stunts");
return next(call, next); // runs the base implementation
})
.ToObject();
greeter.Greet("world"); // "Hello, Stunts"var add = Stunt.For<Func<int, int, int>>((x, y) => x + y)
.AddBehavior((call, next) =>
{
call.Arguments.Set(0, 10);
return next(call, next);
})
.ToObject();
add(1, 2); // 12var recorder = new RecordingBehavior();
var builder = Stunt.Builder()
.AddBehavior(recorder)
.AddBehavior(new DefaultValueBehavior());
ICalculator calculator = builder.Build<ICalculator>();
IServiceProvider services = builder.Build<IServiceProvider>();
calculator.Add(2, 3); // 0, from DefaultValueBehavior
services.GetService(typeof(string)); // null
// recorder.Invocations has the calls of both: one recorder for every stunt the builder built
Stunts is all most projects need. The others add behaviors, switch to run-time proxies, or
give you the pipeline without any proxy generation.
The name comes from test doubles, which in turn come from the stunt doubles of film making. Stunts provides the low-level building blocks; higher-level APIs, such as mocking libraries, are built on top.
Stunts is open source under the MIT license. Its use is subject to the Open Source Maintenance Fee EULA: if you use it to generate revenue, sponsoring is required.
