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

Usefull

Download Example (.NET C# )

Share SyncMethodGenerator

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

In the same category (EnhancementClass) - 25 other generators

ApparatusAOT

AspectGenerator

CommonCodeGenerator

DudNet

Enhanced.GetTypes

FastGenericNew

HsuSgSync

Immutype

Ling.Audit

Lombok.NET

M31.FluentAPI

MemoryPack

Meziantou.Polyfill

Microsoft.Extensions.Logging

Microsoft.Extensions.Options.Generators.OptionsValidatorGenerator

Microsoft.Interop.JavaScript.JSImportGenerator

OptionToStringGenerator

QueryStringGenerator

RSCG_Decorator

RSCG_UtilityTypes

StaticReflection

System.Runtime.InteropServices

System.Text.RegularExpressions

TelemetryLogging

ThisClass