RSCG – HsuSgSync
RSCG – HsuSgSync
name | HsuSgSync |
nuget | https://www.nuget.org/packages/Hsu.Sg.Sync/ |
link | https://github.com/hsu-net/source-generators |
author | Net Hsu |
Generate code for async to sync methods
This is how you can use HsuSgSync .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net8.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Hsu.Sg.Sync" Version="2023.412.21" OutputItemType="Analyzer" > </PackageReference> </ItemGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> </Project>
The code that you will use is
using HsuSgSync; var p=new Person(); var result=await p.RunAsync(); Console.WriteLine(result); result=p.Run();
using System.ComponentModel; using Hsu.Sg.Sync; namespace HsuSgSync; [Sync] internal partial class Person { public async Task<bool> RunAsync() { await Task.Delay(1000); return true; } }
The code that is generated is
// <auto-generated/> using System; namespace Hsu.Sg.Sync { /// <summary> /// The flag to generate async method to sync method. /// </summary> [AttributeUsage( System.AttributeTargets.Interface | System.AttributeTargets.Struct | System.AttributeTargets.Class, AllowMultiple = false, Inherited = false)] internal sealed class SyncAttribute : Attribute { /// <summary> /// Only <c>interface</c> or <c>abstract</c> async methods are generated. /// </summary> public bool Definable { get; set; } /// <summary> /// The public async methods are generated. /// </summary> public bool Public { get; set; } = true; /// <summary> /// The internal async methods are generated. /// </summary> public bool Internal { get; set; } = true; /// <summary> /// The private async methods are generated. /// </summary> public bool Private { get; set; } = true; /// <summary> /// Only [SyncGen] async methods are generated. /// </summary> public bool Only { get; set; } = false; /// <summary> /// The suffix of sync method name when not end with Async. /// </summary> /// <remarks>default is `Sync`</remarks> public string Suffix { get; set; } = string.Empty; /// <summary> /// Whether generate attributes. /// </summary> public bool Attribute { get; set; } = false; /// <summary> /// To generate with attributes /// </summary> public string[] AttributeIncludes { get; set; } = null; /// <summary> /// To generate without attributes /// </summary> public string[] AttributeExcludes { get; set; } = null; public SyncAttribute() { Public = true; Internal = true; Private = true; Only = false; Suffix = string.Empty; } } [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] internal sealed class SyncGenAttribute : Attribute { /// <summary> /// Ignore to generate sync methods. /// </summary> public bool Ignore { get; set; } = false; /// <summary> /// The specific name of sync method. /// </summary> public string Identifier { get; set; } = string.Empty; /// <summary> /// The suffix of sync method name when not end with Async. /// </summary> /// <remarks>default is `Sync`</remarks> public string Suffix { get; set; } = string.Empty; } }
// <auto-generated/> using System.Runtime.CompilerServices; using System.Threading.Tasks; using System; namespace Hsu.Sg.Sync { internal static partial class SyncHelper { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Run(Task task) { Nito.AsyncEx.AsyncContext.Run(async () => await task); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Run<T>(Task<T> task) { return Nito.AsyncEx.AsyncContext.Run(async () => await task); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Run(Func<Task> task) { Nito.AsyncEx.AsyncContext.Run(async () => await task()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Run<T>(Func<Task<T>> task) { return Nito.AsyncEx.AsyncContext.Run(async () => await task()); } } }
// <auto-generated/> using System.Runtime.CompilerServices; using System.Threading.Tasks; using System; namespace Hsu.Sg.Sync { internal static partial class SyncHelper { [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Run(ValueTask task) { Nito.AsyncEx.AsyncContext.Run(async () => await task); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Run<T>(ValueTask<T> task) { return Nito.AsyncEx.AsyncContext.Run(async () => await task); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Run(Func<ValueTask> task) { Nito.AsyncEx.AsyncContext.Run(async () => await task()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Run<T>(Func<ValueTask<T>> task) { return Nito.AsyncEx.AsyncContext.Run(async () => await task()); } } }
// <auto-generated/> // Generated 1 sync methods by SyncGenerator // ValueTaskSupported : True // DefaultImplementationsOfInterfacesSupported : True // Metadata { Only = False, Definable = False, Public = True, Internal = True, Private = True, Attribute = False } using System.ComponentModel; namespace HsuSgSync; internal partial class Person { /// <inheritdoc cref="RunAsync()" /> /// <remarks></remarks> public bool Run() => Hsu.Sg.Sync.SyncHelper.Run(() => RunAsync()); }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/HsuSgSync