HsuSgSync by Net Hsu
Nuget / site data
Details
Info
info
Name: HsuSgSync
an async method to sync source generator.
Author: Net Hsu
NuGet: https://www.nuget.org/packages/Hsu.Sg.Sync/
You can find more details at https://github.com/hsu-net/source-generators
Original Readme
note
Hsu.Sg
.NET source generators
Package Version
Name | Source | Stable | Preview |
---|---|---|---|
Hsu.Sg.Sync | Nuget | ||
Hsu.Sg.Sync | MyGet | ||
Hsu.Sg.Proxy | Nuget | ||
Hsu.Sg.Proxy | MyGet | ||
Hsu.Sg.FluentMember | Nuget | ||
Hsu.Sg.FluentMember | MyGet |
Package Features
Hsu.Sg.Sync
Generate a synchronous method from an asynchronous method.
Usages see README
Hsu.Sg.Proxy
Generate a proxy object from a struct
or class
or interface
.
Usages see README
Hsu.Sg.FluentMember
Generate a fluent method from a struct
or class
.
Usages see README
References
License
About
note
Generate code for async to sync methods
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references HsuSgSync
<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>
This is the use of HsuSgSync in Program.cs
using HsuSgSync;
var p=new Person();
var result=await p.RunAsync();
Console.WriteLine(result);
result=p.Run();
This is the use of HsuSgSync in Person.cs
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;
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- Hsu.Sg.Sync.SyncAttribute.g.cs
- Hsu.Sg.Sync.SyncHelper.g.cs
- Hsu.Sg.Sync.SyncHelper.ValueTask.g.cs
- HsuSgSync.Person.sync.g.cs
// <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());
}
Usefull
Download Example (.NET C# )
Share HsuSgSync
https://ignatandrei.github.io/RSCG_Examples/v2/docs/HsuSgSync