SyncMethodGenerator by Zomp Inc.
NuGet / site data
Details
Info
Name: SyncMethodGenerator
Sync Method Generator
Author: Zomp Inc.
NuGet: https://www.nuget.org/packages/Zomp.SyncMethodGenerator/
You can find more details at https://github.com/zompinc/sync-method-generator
Original Readme
Sync Method Generator
This .NET source generator produces a sync method from an async one.
Use cases
- A library which exposes both sync and async version of a method
- An application has to process two kinds of data in the same way:
- Large data from I/O which cannot be stored in memory before processing: Original async method
- Small sample of data in memory, usually a sample of the larger data: Generated sync method
How it works
Add CreateSyncVersionAttribute to your async method in your partial class
[Zomp.SyncMethodGenerator.CreateSyncVersion]
static async Task WriteAsync(ReadOnlyMemory<byte> buffer, Stream stream,
CancellationToken ct)
=> await stream.WriteAsync(buffer, ct).ConfigureAwait(true);
And it will generate a sync version of the method:
static void Write(ReadOnlySpan<byte> buffer, Stream stream)
=> stream.Write(buffer);
A list of changes applied to the new synchronized method:
-
Remove async modifier
-
Remove await from methods as well as
foreachstatement -
Change types
From To Task or ValueTask void Task or ValueTask T IAsyncEnumerable IEnumerable IAsyncEnumerator IEnumerator Memory Span ReadOnlyMemory ReadOnlySpan -
Remove parameters
-
Invocation changes
- Remove ConfigureAwait
- Remove WithCancellation
- Remove
Asyncsuffix from method calls (e.g. MoveNextAsync becomes MoveNext) - Remove CancellationToken parameter
- Remove IProgress.Report(T) call
- Remove Memory.Span property
-
Remove
CreateSyncVersionAttribute -
Update XML documentation
Installation
To add the library use:
dotnet add package Zomp.SyncMethodGenerator
About
Generating Sync method from async
How to use
Example (source csproj, source files)
- CSharp Project
- Program.cs
- Writer.cs
This is the CSharp Project that references SyncMethodGenerator
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Zomp.SyncMethodGenerator" Version="1.0.14" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of SyncMethodGenerator in Program.cs
// See https://aka.ms/new-console-template for more information
using Zomp.SyncMethodGeneratorDemo;
Console.WriteLine("Hello, World!");
Writer.Haha("a.txt", "Andrei Ignat");
Writer.Write("a.txt", "andrei ignat");
This is the use of SyncMethodGenerator in Writer.cs
namespace Zomp.SyncMethodGeneratorDemo;
partial class Writer
{
[Zomp.SyncMethodGenerator.CreateSyncVersion]
public static async Task WriteAsync(string file, string contents,
CancellationToken ct)
{
await File.WriteAllTextAsync(file, contents, ct).ConfigureAwait(true);
}
[Zomp.SyncMethodGenerator.CreateSyncVersion]
public static async Task HahaAsync(ReadOnlyMemory<byte> buffer, Stream stream,
CancellationToken ct)
=> await stream.WriteAsync(buffer, ct).ConfigureAwait(true);
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- CreateSyncVersionAttribute.g.cs
- Zomp.SyncMethodGeneratorDemo.Writer.HahaAsync.g.cs
- Zomp.SyncMethodGeneratorDemo.Writer.WriteAsync.g.cs
// <auto-generated/>
namespace Zomp.SyncMethodGenerator
{
/// <summary>
/// An attribute that can be used to automatically generate a synchronous version of an async method. Must be used in a partial class.
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Method)]
internal class CreateSyncVersionAttribute : System.Attribute
{
}
}
// <auto-generated/>
#nullable enable
namespace Zomp.SyncMethodGeneratorDemo;
partial class Writer
{
public static void Haha(global::System.ReadOnlySpan<byte> buffer, global::System.IO.Stream stream)
=> stream.Write(buffer);
}
// <auto-generated/>
#nullable enable
namespace Zomp.SyncMethodGeneratorDemo;
partial class Writer
{
public static void Write(string file, string contents)
{
global::System.IO.File.WriteAllText(file, contents);
}
}
Useful
Download Example (.NET C#)
Share SyncMethodGenerator
https://ignatandrei.github.io/RSCG_Examples/v2/docs/SyncMethodGenerator
aaa
Category "EnhancementClass" has the following generators:
1 AOP.Logging.SourceGenerator
2026-08-30
2 ApparatusAOT
2023-04-16
3 AspectGenerator
2024-01-07
4 CommonCodeGenerator
2024-04-03
5 Comparison
2025-05-25
6 DudNet
2023-10-27
7 Enhanced.GetTypes
2024-09-17
8 FastGenericNew
2023-08-10
9 Immutype
2023-08-12
10 Ling.Audit
2023-12-12
11 Lombok.NET
2023-04-16
12 M31.FluentAPI
2023-08-25
13 MemberAccessor
2025-03-24
14 MemoryPack
2023-08-04
15 Meziantou.Polyfill
2023-10-10
16 Microsoft.Extensions.Logging
2023-04-16
17 Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator
2023-11-17
18 Microsoft.Interop.JavaScript.JSImportGenerator 2023-04-16
19 NLog.Extensions.ThisClass
2026-04-03
20 OptionToStringGenerator
2024-02-15
21 Pekspro.DataAnnotationValuesExtractor
2026-02-15
22 Program
2025-11-06
23 QueryStringGenerator
2024-11-07
24 RSCG_Decorator
2023-09-30
25 RSCG_UtilityTypes
2023-12-22
26 StaticReflection
2023-10-13
27 SyncMethodGenerator
2023-08-14
28 System.Runtime.InteropServices
2023-04-16
29 System.Text.RegularExpressions
2023-04-16
30 TelemetryLogging
2023-11-30
31 Tenekon.MethodOverloads.SourceGenerator
2026-09-05
32 ThisClass
2024-04-19
33 TrimItEasy
2026-08-28