AsyncRewriter

Thong Nguyen edited this page Jul 12, 2016 · 1 revision

Introduction

Shaolinq.AsynRewriter automatically converts your sync methods into async methods using the Roslyn compiler to parse and generate equivalent async code. Library developers quickly realise that the code for async is pretty much the same as their sync code except that calls to Method1() are replaced with await Method1Async() calls which results in a lot of copy & paste code. Shaolinq.AsyncRewriter solves this issue by generating the code for you.

Installation

Shaolinq.AsyncRewriter is available as a NuGet package (Shaolinq.AsyncRewriter). Create a new class library project and then add the Shaolinq.AsyncRewriter NuGet Package. The package will automatically add some pre-build steps to your csproj to generate the async code before the full compile. Additionally the package will add a class named RewriteAsyncAttribute to your project. Simply decorate any method in your project with the [RewriteAsync] attribute to have an equivalent async method generated. You will need to make sure any types containing [RewriteAsync] are declared as partial because AsyncRewriter will add async methods to the type by outputting matching partial classes.

How it works

AsyncRewriter will generate a source file named GeneratedAsync.cs. All types with methods attributed with [RewriteAsync] will get equivalent async methods (two async methods for each sync method) outputted into GeneratedAsync.cs.

All methods are translated as follows:

For each attributed sync method two methods are generated with the name and "Async" appended.