RSCG – AsyncIt
| name | AsyncIt |
| nuget | https://www.nuget.org/packages/AsyncIt/ |
| link | https://github.com/oleg-shilo/AsyncIt/ |
| author | Oleg Shilo |
Generate async from sync or sync from async
This is how you can use AsyncIt .
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>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AsyncIt" Version="1.0.0-pre4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
The code that you will use is
using AsyncDemo; var p=new Person(); var result=await p.RunAsync(); Console.WriteLine(result); result=p.Run(); Console.WriteLine(result);
using System.ComponentModel;
namespace AsyncDemo;
[AsyncIt.Async(Interface = AsyncIt.Interface.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;
using System.Reflection;
namespace AsyncIt
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class AsyncAttribute : Attribute
{
public AsyncAttribute()
{
}
public AsyncAttribute(Algorithm algorithm, Interface @interface)
{
Algorithm = algorithm;
Interface = @interface;
}
public AsyncAttribute(Interface @interface, Algorithm algorithm)
{
Algorithm = algorithm;
Interface = @interface;
}
public AsyncAttribute(Algorithm algorithm)
{
Algorithm = algorithm;
}
public AsyncAttribute(Interface @interface)
{
Interface = @interface;
}
public Algorithm Algorithm { get; set; }
public Interface Interface { get; set; }
internal string TypeGenericArgs;
internal string NamePattern;
}
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public sealed class AsyncExternalAttribute : Attribute
{
public AsyncExternalAttribute()
{
}
public AsyncExternalAttribute(Type type)
{
Type = type;
}
public AsyncExternalAttribute(Type type, Interface @interface)
{
Type = type;
Interface = @interface;
}
public AsyncExternalAttribute(Type type, Interface @interface, string methods)
{
Type = type;
Interface = @interface;
Methods = methods;
}
public Interface Interface { get; set; }
public Type Type { get; set; }
public string Methods { get; set; } = "*";
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class IgnoreAttribute : Attribute
{
}
public enum Interface
{
Async,
Sync,
Full,
}
public enum Algorithm
{
PartialType,
ExtensionMethods
}
}
// <auto-generated/>
using System.ComponentModel;
namespace AsyncDemo
{
internal partial class Person
{
/// <summary>
/// The synchronous version of <see cref="Person.RunAsync()"/>.
/// </summary>
public bool Run()
=> RunAsync().Result;
}
}
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/AsyncIt