Skip to main content

SyncMethodGenerator by Zomp Inc.

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

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

Source: https://github.com/zompinc/sync-method-generator

Original Readme

note

Sync Method Generator

Build Support .NET Standard 2.0 Nuget codecov

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:

Installation

To add the library use:

dotnet add package Zomp.SyncMethodGenerator

About

note

Generating Sync method from async

How to use

Example (source csproj, source files)

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>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

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

Useful

Download Example (.NET C#)

Share SyncMethodGenerator

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

aaa

Category "EnhancementClass" has the following generators:

1 ApparatusAOT Nuget GitHub Repo stars 2023-04-16

2 AspectGenerator Nuget GitHub Repo stars 2024-01-07

3 CommonCodeGenerator Nuget GitHub Repo stars 2024-04-03

4 Comparison Nuget GitHub Repo stars 2025-05-25

5 DudNet Nuget GitHub Repo stars 2023-10-27

6 Enhanced.GetTypes Nuget GitHub Repo stars 2024-09-17

7 FastGenericNew Nuget GitHub Repo stars 2023-08-10

8 Immutype Nuget GitHub Repo stars 2023-08-12

9 Ling.Audit Nuget GitHub Repo stars 2023-12-12

10 Lombok.NET Nuget GitHub Repo stars 2023-04-16

11 M31.FluentAPI Nuget GitHub Repo stars 2023-08-25

12 MemberAccessor Nuget GitHub Repo stars 2025-03-24

13 MemoryPack Nuget GitHub Repo stars 2023-08-04

14 Meziantou.Polyfill Nuget GitHub Repo stars 2023-10-10

15 Microsoft.Extensions.Logging Nuget GitHub Repo stars 2023-04-16

16 Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator Nuget GitHub Repo stars 2023-11-17

17 Microsoft.Interop.JavaScript.JSImportGenerator 2023-04-16

18 OptionToStringGenerator Nuget GitHub Repo stars 2024-02-15

19 Program Nuget GitHub Repo stars 2025-11-06

20 QueryStringGenerator Nuget GitHub Repo stars 2024-11-07

21 RSCG_Decorator Nuget GitHub Repo stars 2023-09-30

22 RSCG_UtilityTypes NugetNuget GitHub Repo stars 2023-12-22

23 StaticReflection NugetNuget GitHub Repo stars 2023-10-13

24 SyncMethodGenerator Nuget GitHub Repo stars 2023-08-14

25 System.Runtime.InteropServices Nuget GitHub Repo stars 2023-04-16

26 System.Text.RegularExpressions Nuget GitHub Repo stars 2023-04-16

27 TelemetryLogging Nuget GitHub Repo stars 2023-11-30

28 ThisClass Nuget GitHub Repo stars 2024-04-19

See category

EnhancementClass