Skip to main content

Splice by Tyrrx

NuGet / site data​

Nuget GitHub last commit GitHub Repo stars

Details​

Info​

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​

note

Tyrrx Alt text

Original Readme​

note

Splice 🧬​

GitHub License GitHub Release GitHub Actions Workflow Status

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")]


2. Generic-based (Clean syntax)
[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:

  1. Automated build and test.
  2. Creation of a GitHub Release with build artifacts.
  3. 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​

note

Intercept method calls at compile time using C# 14 interceptors

How to use​

Example (source csproj, source files)​

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>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

// <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) {}
}
}

Useful​

Download Example (.NET C#)​

Share Splice​

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Splice

Category "AOP" has the following generators:​

1 Splice Nuget GitHub Repo stars 2026-11-20

2 WhatIAmDoing NugetNuget GitHub Repo stars 2024-01-28

3 ZeroAlloc.Resilience Nuget GitHub Repo stars 2026-11-17

See category​

AOP