Splice by Tyrrx
NuGet / site data
Details
Info
Name: Splice
A C# .NET library for Roslyn Source Generators implementing C# 14 Interceptors.
Author: Tyrrx
NuGet: https://www.nuget.org/packages/Splice/
You can find more details at https://github.com/Tyrrx/Splice
Source: https://github.com/Tyrrx/Splice
Author
Tyrrx

Original Readme
Splice 🧬
A C# .NET solution focused on Roslyn Source Generators, specifically implementing C# 14 "Interceptors". This framework allows you to easily intercept and replace method calls at compile time.
Quick Start 🏁
######### 1. Installation 📦
Get Splice from NuGet: Splice.
Add the Splice as a source generator to your project. If you are using NuGet:
dotnet add package Splice
Or via the .csproj:
<PackageReference Include="Splice" Version="0.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
######### 2. Project Configuration ⚙️
Interceptors are a C# 11+ feature (fully matured in C# 14). You need to enable them in your .csproj (C# 12+ required for basic support, C# 14 for full stability):
<PropertyGroup>
<!-- Required: Interceptors are a preview/new feature (C# 14.0 or preview) -->
<LangVersion>preview</LangVersion>
<!-- Required: You must explicitly allow namespaces to contain interceptors -->
<InterceptorsNamespaces>$(InterceptorsNamespaces);MyProject.Interceptors</InterceptorsNamespaces>
<!-- Optional: Useful for debugging generated code -->
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
How to Use 🛠️
######### Create an Interceptor
To intercept a method, create a partial class and a partial method decorated with the [Interceptor] attribute.
using System;
using Generators; // Namespace for the [Interceptor] attribute
namespace MyProject.Interceptors;
public static partial class MyInterceptor
{
// Intercept Console.WriteLine(string) calls
[Interceptor(typeof(Console), nameof(Console.WriteLine))]
public static partial void WriteLine(string? message)
{
// NOTE: Calls to the intercepted method within its OWN body are NOT intercepted.
// However, if this method calls ANOTHER method that then calls Console.WriteLine,
// ⚠️ you might run into infinite recursion!
Console.WriteLine($"[Intercepted] {message}");
}
}
######### Attribute Variations
The framework provides two ways to specify the target method:
1. Type-based (Recommended for non-generic targets)
[Interceptor(typeof(TargetType), "MethodName")]
[Interceptor<TargetType>("MethodName")]
Configuration Details ⚙️
######### The InterceptorsNamespaces Property
The C# compiler requires you to explicitly list the namespaces that are allowed to contain interceptors for security and performance reasons.
If your interceptor is in MyProject.Interceptors, you must add that namespace to the <InterceptorsNamespaces> property in your .csproj. You can add multiple namespaces separated by semicolons.
<InterceptorsNamespaces>$(InterceptorsNamespaces);Namespace1;Namespace2</InterceptorsNamespaces>
Features ✨
- Core Interceptor Source Generator: Automatically maps interceptors to their call sites.
- Recursion Protection: Prevents an interceptor from intercepting itself (only within its own method body).
- Sample project: Real-world demonstration of interception.
- Comprehensive test suite: Using xUnit v3 to ensure stability.
Automated Workflows 🤖
######### Continuous Integration (CI)
The project uses GitHub Actions to automatically build and test every push and pull request targeting the main branch.
######### Continuous Deployment (CD)
Releases are automated via GitHub Actions. Pushing a tag matching v* (e.g., v1.0.0) triggers:
- Automated build and test.
- Creation of a GitHub Release with build artifacts.
- Publishing the package to NuGet.org.
Note: To publish to NuGet, a NUGET_API_KEY must be configured in the repository's GitHub Secrets.
About
Intercept method calls at compile time using C# 14 interceptors
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- Person.cs
- PersonInterceptors.cs
This is the CSharp Project that references Splice
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
<InterceptorsNamespaces>$(InterceptorsNamespaces);SpliceDemo</InterceptorsNamespaces>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Splice" Version="0.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
This is the use of Splice in Program.cs
using SpliceDemo;
var person = new Person
{
FirstName = "Ignat",
LastName = "Andrei"
};
Console.WriteLine(person.FullName());
This is the use of Splice in Person.cs
namespace SpliceDemo;
public class Person
{
public string FirstName \{ get; set; \} = string.Empty;
public string LastName \{ get; set; \} = string.Empty;
public string FullName()
{
return $"{FirstName} {LastName}";
}
}
This is the use of Splice in PersonInterceptors.cs
using Splice;
namespace SpliceDemo;
public static partial class PersonInterceptors
{
[Interceptor(typeof(Person), nameof(Person.FullName))]
public static partial string InterceptFullName(this Person person)
{
return $"Intercepted: {person.FirstName} {person.LastName}";
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- InterceptorAttribute.g.cs
- SpliceDemo.PersonInterceptors.InterceptFullName.g.cs
// <auto-generated/>
namespace Splice
{
[System.AttributeUsage(System.AttributeTargets.Method)]
public class InterceptorAttribute<T> : System.Attribute
{
public string Value \{ get; }
public InterceptorAttribute(string value) => Value = value;
}
[System.AttributeUsage(System.AttributeTargets.Method)]
public class InterceptorAttribute : System.Attribute
{
public string Value \{ get; }
public InterceptorAttribute(System.Type type, string methodName) => Value = methodName;
}
}
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public sealed class InterceptsLocationAttribute : Attribute
{
public InterceptsLocationAttribute(int version, string data) {}
}
}
// <auto-generated/>
#nullable enable
using System.Runtime.CompilerServices;
namespace SpliceDemo;
public partial class PersonInterceptors
{
[InterceptsLocation(version: 1, data: "9DKI4o9W3a8ZSE3JDek9koMAAABQcm9ncmFtLmNz")]
public static partial string InterceptFullName(this Person person);
}
Useful
Download Example (.NET C#)
Share Splice
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Splice
Category "AOP" has the following generators:
1 Splice
2026-11-20
2 WhatIAmDoing
2024-01-28
3 ZeroAlloc.Resilience
2026-11-17