Zomp.MethodCloning.Variants by Zomp Inc.
NuGet / site data
Details
Info
Name: Zomp.MethodCloning.Variants
A source generator which writes the variants of a method from the one written by hand: SaveAsBmp becomes SaveAsGif and SaveAsPng, with their types and documentation. Replaces T4 templates which stamp out the same method once per type.
Author: Zomp Inc.
NuGet: https://www.nuget.org/packages/Zomp.MethodCloning.Variants/
You can find more details at https://github.com/zompinc/method-cloning
Author
Zomp Inc.

Original Readme
Zomp.MethodCloning
Roslyn source generators that copy methods, so you write each one once.
| I want to... | Use |
|---|---|
| Stop maintaining T4 templates that repeat a method per format or type | Zomp.MethodCloning.Variants |
| Write my own generator that copies methods and changes them | Zomp.MethodCloning |
Zomp.SyncMethodGenerator, which writes the sync version of async methods, is built on the same core.
Learn more
- Variants reference: every rule, the limitations, and migrating from T4.
- AGENTS.md: how the repository is built and tested, and the rules the core's source files follow.
Build
dotnet build
dotnet test
About
Generate format, type, or mode variants from one written method.
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- ImageExtensions.cs
- Formats.cs
This is the CSharp Project that references Zomp.MethodCloning.Variants
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Zomp.MethodCloning.Variants" Version="1.0.24">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of Zomp.MethodCloning.Variants in Program.cs
using MethodCloningDemo;
using MethodCloningDemo.Formats.Bmp;
using MethodCloningDemo.Formats.Gif;
using MethodCloningDemo.Formats.Png;
var image = new Image();
ImageExtensions.SaveAsBmp(image, new BmpEncoder());
ImageExtensions.SaveAsGif(image, new GifEncoder());
ImageExtensions.SaveAsPng(image, new PngEncoder());
This is the use of Zomp.MethodCloning.Variants in ImageExtensions.cs
using MethodCloningDemo.Formats.Bmp;
using Zomp.MethodCloning;
namespace MethodCloningDemo;
public class Image
{
}
public static partial class ImageExtensions
{
/// <summary>Saves the image in the Bmp format.</summary>
[CloneVariants("Bmp", "Gif", "Png")]
public static void SaveAsBmp(Image image, BmpEncoder encoder)
{
Console.WriteLine($"Saving {BmpFormat.Instance.Name}: {encoder.Name}");
}
}
This is the use of Zomp.MethodCloning.Variants in Formats.cs
namespace MethodCloningDemo.Formats.Bmp
{
public sealed class BmpEncoder
{
public string Name => "BMP encoder";
}
public sealed class BmpFormat
{
public static BmpFormat Instance \{ get; \} = new();
public string Name => "BMP";
}
}
namespace MethodCloningDemo.Formats.Gif
{
public sealed class GifEncoder
{
public string Name => "GIF encoder";
}
public sealed class GifFormat
{
public static GifFormat Instance \{ get; \} = new();
public string Name => "GIF";
}
}
namespace MethodCloningDemo.Formats.Png
{
public sealed class PngEncoder
{
public string Name => "PNG encoder";
}
public sealed class PngFormat
{
public static PngFormat Instance \{ get; \} = new();
public string Name => "PNG";
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- CloneVariantsAttribute.g.cs
- MethodCloningDemo.ImageExtensions.SaveAsBmp.Gif.g.cs
- MethodCloningDemo.ImageExtensions.SaveAsBmp.Png.g.cs
// <auto-generated/>
namespace Zomp.MethodCloning
{
/// <summary>
/// Writes variants of a method from the one written by hand. The word naming the variant the
/// method was written for is swapped for each of the others: in the name of the method, in the
/// types and static members it refers to, and in its documentation.
/// </summary>
/// <remarks>
/// Applied to a partial type, it marks every method in the same declaration of the type.
/// </remarks>
[global::System.AttributeUsage(
global::System.AttributeTargets.Method |
global::System.AttributeTargets.Class |
global::System.AttributeTargets.Struct |
global::System.AttributeTargets.Interface,
AllowMultiple = false,
Inherited = false)]
[global::System.Diagnostics.Conditional("ZOMP_METHOD_CLONING_ATTRIBUTES")]
internal sealed class CloneVariantsAttribute : global::System.Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="CloneVariantsAttribute"/> class.
/// </summary>
/// <param name="original">The word naming the variant the method is written for, such as <c>Bmp</c>.</param>
/// <param name="variants">The words naming the variants to write, such as <c>Gif</c> and <c>Png</c>.</param>
public CloneVariantsAttribute(string original, params string[] variants)
{
}
}
}
// <auto-generated/>
#nullable enable
using MethodCloningDemo.Formats.Gif;
using Zomp.MethodCloning;
namespace MethodCloningDemo;
public static partial class ImageExtensions
{
/// <summary>Saves the image in the Gif format.</summary>
public static void SaveAsGif(global::MethodCloningDemo.Image image, global::MethodCloningDemo.Formats.Gif.GifEncoder encoder)
{
global::System.Console.WriteLine($"Saving {(global::MethodCloningDemo.Formats.Gif.GifFormat.Instance.Name)}: {(encoder.Name)}");
}
}
// <auto-generated/>
#nullable enable
using MethodCloningDemo.Formats.Png;
using Zomp.MethodCloning;
namespace MethodCloningDemo;
public static partial class ImageExtensions
{
/// <summary>Saves the image in the Png format.</summary>
public static void SaveAsPng(global::MethodCloningDemo.Image image, global::MethodCloningDemo.Formats.Png.PngEncoder encoder)
{
global::System.Console.WriteLine($"Saving {(global::MethodCloningDemo.Formats.Png.PngFormat.Instance.Name)}: {(encoder.Name)}");
}
}
Useful
Download Example (.NET C#)
Share Zomp.MethodCloning.Variants
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Zomp.MethodCloning.Variants
Category "Templating" has the following generators:
1 Gobie
2023-08-22
2 InlineComposition
2025-07-22
3 InterceptorTemplate
2023-11-29
4 JKToolKit.TemplatePropertyGenerator
2024-07-17
5 Microsoft.NET.Sdk.Razor.SourceGenerators 2023-04-16
6 NTypewriter
2025-01-19
7 RazorBlade
2023-04-16
8 RazorSlices
2024-10-27
9 RSCG_IFormattable
2024-06-29
10 RSCG_Templating
2023-10-07
11 spreadcheetah
2023-08-13
12 Tortuga.Shipwright
2025-07-21
13 Zomp.MethodCloning.Variants
2026-11-10